Searching an array costs O(n) and a balanced tree costs O(log n). Hashing jumps straight to the answer: the key itself computes the slot it lives in.
1 Index math, not search
A hash function turns any key into a bucket number: hash(key) % buckets. Lookup becomes one jump — no scanning.
2 The hash function
Same key must always give the same slot, and different keys should spread out. key % 7 is the classic teaching hash.
3 Collisions happen
7 buckets cannot hold every key uniquely: 33 % 7 and 12 % 7 both land in slot 5. Chaining and probing are the two classic fixes.
4 std::unordered_map
The STL hash table: find, insert and erase in O(1) average. Keys come back in no guaranteed order.
5 std::map
The sorted cousin: a balanced tree with O(log n) operations and keys that iterate in order.
6 Load factor
size / bucket_count. Let it creep up and chains and probes get long — implementations rehash into a bigger table.
Why O(1) lookup
Index math, not search1A hash function turns any key into a bucket number: hash(key) % buckets. Lookup becomes one jump — no scanning. The hash function2Same key must always give the same slot, and different keys should spread out. key % 7 is the classic teaching hash. Collisions happen37 buckets cannot hold every key uniquely: 33 % 7 and 12 % 7 both land in slot 5. Chaining and probing are the two classic fixes. std::unordered_map4The STL hash table: find, insert and erase in O(1) average. Keys come back in no guaranteed order. std::map5The sorted cousin: a balanced tree with O(log n) operations and keys that iterate in order. Load factor6size / bucket_count. Let it creep up and chains and probes get long — implementations rehash into a bigger table. 6 building blocks make up Why O(1) lookup
Six keys arrive at a 7-bucket table. hash(key) = key % 7 picks each slot — and one pair of keys collides into a chain.
1 / 10A hash table with 7 buckets. The hash function is key % 7: any key lands in bucket key % 7. Everything starts empty.
Every unordered_map lookup is this: one hash jump, then a short walk only if the bucket chains.
process decision input / output start / end unordered_map in action — insert, lookup, overwrite, erase — then std::map showing off its sorted keys.
mainAn empty hash map: string keys, int values. Under the hood — buckets plus a hash function.
Console
output appears as the program runs…
Same keys as before, different collision plan: open addressing keeps every key inside the array and probes rightward on a clash.
1 / 10Same keys, different collision plan: open addressing keeps every key inside the array itself — no chains, just slots.
A frequency counter with one sneaky line — operator[] has a side effect.
Your predicted output — one line per console line
Hint: operator[] on a missing key silently INSERTS it with value 0 — so freq['z'] both prints 0 AND grows the map. size() counts keys, not characters.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?collisiondrops into place
?chainingdrops into place
?linear probingdrops into place
?O(1) averagedrops into place
?std::mapdrops into place
?load factordrops into place
?operator[]drops into place
01A hash table turns key lookup into index math — O(1) on average.
02hash(key) must be fast and spread keys evenly; key % buckets is the classic teaching hash.
03Collisions are normal: chaining keeps a list per bucket, probing hunts the next free slot.
04std::unordered_map: hash table, O(1) average, no guaranteed iteration order.
05std::map: balanced tree, O(log n), and iteration is always key-sorted.
06operator[] inserts missing keys with 0 — use count() or find() to test membership safely.