unordered_map in C++

πΉ What is unordered_map
?
A key β value container from C++ STL.
Similar to
map
, but does NOT store keys in sorted order.Keys are stored in buckets using hashing.
Provides average O(1) lookup, insert, erase (faster than
map
).Order of keys is unpredictable.
π Think of it as a hash table implementation of a dictionary.
πΉ Syntax
unordered_map<KeyType, ValueType> mp;
Example:
unordered_map<string, int> age;
age["Alice"] = 20;
age["Bob"] = 25;
Storage (order is not guaranteed):
"Bob" β 25
"Alice" β 20
πΉ Common Operations & Complexity
Operation | Code Example | Avg Time | Worst Time |
Insert | mp[key] = value; or mp.insert({key,value}); | O(1) | O(n) |
Access (by key) | mp[key] | O(1) | O(n) |
Search | mp.find(key) | O(1) | O(n) |
Erase | mp.erase(key) | O(1) | O(n) |
Size | mp.size() | O(1) | O(1) |
Iterate | for (auto &p: mp) ... | O(n) | O(n) |
β οΈ Worst-case O(n) happens if many keys land in the same bucket (hash collisions). But in practice, itβs rare.
πΉ Example
unordered_map<int, string> mp;
mp[30] = "X";
mp[10] = "Y";
mp[20] = "Z";
// Order is not guaranteed!
// Could print: 20βZ, 30βX, 10βY
πΉ Checking Existence
if (mp.find(20) != mp.end()) {
cout << "Key 20 found, value = " << mp[20];
}
else {
cout << "Key 20 not found";
}
πΉ Iteration
for (auto &p : mp) {
cout << p.first << " β " << p.second << endl;
}
β οΈ Unlike map
, iteration order is unpredictable (depends on hashing).
πΉ Characteristics
Unordered β No sorting of keys.
Unique keys β Each key is stored only once.
Underlying data structure β Hash Table.
Fast average operations β O(1).
πΉ Advantages (Pros)
β
Much faster than map
for lookups (average O(1)).
β
Great for frequency counting, existence check, hash-based problems.
β
Easy syntax like map
.
πΉ Disadvantages (Cons)
β Keys are not stored in order β no range queries, no smallest/largest key.
β Slightly more memory usage due to hashing buckets.
β Worst-case O(n) time (though rare with good hash functions).
β Doesnβt work well with custom complex types unless you define a hash function.
πΉ map
vs unordered_map
Feature | map (Red-Black Tree) | unordered_map (Hash Table) |
Order of keys | Sorted | No order |
Lookup/Insert/Erase | O(log n) | O(1) average, O(n) worst |
Use case | Need ordering, ranges | Just need fast lookups |
Memory | Lower | Higher (hash overhead) |
πΉ Typical LeetCode Use Cases
Two Sum (store number β index for O(n) solution).
Counting frequency of elements.
unordered_map<int,int> freq; for (int x : nums) freq[x]++;
Checking existence in O(1).
Prefix sums / subarray problems.
Graph adjacency list (fast access).
β Summary
unordered_map
= Hash table based key-value store.Operations = O(1) average.
Faster than
map
if you donβt care about sorting.Use in LeetCode most of the time unless the problem needs sorted keys.
π So:
Use
unordered_map
by default for speed.Use
map
if you specifically need sorted keys or range queries.
Subscribe to my newsletter
Read articles from Abhinav Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abhinav Kumar
Abhinav Kumar
π B.Tech CSE | π¨βπ» Learning DSA & C++ | π Building projects & writing what I learn | π Currently solving LeetCode & exploring Git/GitHub