unordered_set
Hash-based unique elements.
unordered_set is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is unordered_set?
std::unordered_set stores unique elements in a hash table. Membership tests are constant time on average, but there is no sorted order.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s{1, 2, 3, 2, 1};
std::cout << s.size() << " unique values\n";
return 0;
}set vs unordered_set
Like maps:
set: sorted, O(log n).unordered_set: unordered, O(1) average.
Pick unordered_set for the fastest membership checks.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<std::string> seen{"a", "b", "c"};
std::cout << (seen.count("b") ? "yes" : "no") << '\n';
return 0;
}Inserting Values
insert() adds an element, ignoring it if already present, and returns a pair whose .second tells if it was added.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s;
auto a = s.insert(5);
auto b = s.insert(5);
std::cout << std::boolalpha << a.second << ' ' << b.second << '\n';
return 0;
}Fast Membership Tests
Checking whether you have seen a value is the classic use case. count() returns 0 or 1.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<std::string> blocked{"spam", "junk"};
std::cout << blocked.count("spam") << '\n';
std::cout << blocked.count("ok") << '\n';
return 0;
}Erasing Elements
erase() removes a value and returns how many were removed (0 or 1).
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s{1, 2, 3};
s.erase(2);
std::cout << "count 2: " << s.count(2) << '\n';
std::cout << "size: " << s.size() << '\n';
return 0;
}Detecting Duplicates
You can detect the first duplicate in a stream by inserting and checking the boolean result.
#include <iostream>
#include <unordered_set>
int main() {
int data[] = {3, 7, 1, 7, 9};
std::unordered_set<int> seen;
for (int x : data) {
if (!seen.insert(x).second) {
std::cout << "first duplicate: " << x << '\n';
break;
}
}
return 0;
}Iterating
Iteration works but order is unspecified. Sum or process elements without assuming any sequence.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s{10, 20, 30};
int total = 0;
for (int x : s) total += x;
std::cout << "sum = " << total << '\n';
return 0;
}De-duplicating a Range
Construct an unordered_set from a range to drop duplicates quickly (order not preserved).
#include <iostream>
#include <unordered_set>
#include <vector>
int main() {
std::vector<int> v{1, 2, 2, 3, 3, 3};
std::unordered_set<int> u(v.begin(), v.end());
std::cout << u.size() << " unique\n";
return 0;
}find vs count
find() gives you an iterator to the element so you can use it further, while count() just reports presence.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<std::string> s{"alpha", "beta"};
auto it = s.find("beta");
std::cout << (it != s.end() ? *it : "none") << '\n';
return 0;
}Clear and Empty
clear() removes all elements and empty() checks for none.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> s{1, 2, 3};
s.clear();
std::cout << std::boolalpha << s.empty() << '\n';
return 0;
}Set Intersection
To find common elements, loop over one set and test membership in the other.
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> a{1, 2, 3, 4};
std::unordered_set<int> b{3, 4, 5};
for (int x : a) if (b.count(x)) std::cout << x << ' ';
std::cout << '\n';
return 0;
}Quick Check
Test your understanding of unordered_set.
Recap
You learned that std::unordered_set:
- stores unique elements with O(1) average operations
- has no guaranteed order
- is ideal for fast membership tests and duplicate detection
Next, you'll learn to hash your own custom types.
Frequently asked questions
Is the “unordered_set” lesson free?
Yes — the full text of “unordered_set” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “unordered_set”?
Hash-based unique elements. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “unordered_set” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.