Why hash tables exist
Finding one name among a million should not take a million steps. Watch the slow way, pick a faster one, and see why every language has a hash table built in.
4 steps · about 5 minutes · free, no sign-up
Step 1: A million contacts
Someone is calling. Find "Maya" among your 1,000,000 contacts, checking one by one.
Not Ana, not Ben, not Chen…
…still going…
Maya is contact number 812,403. That is 812,403 checks to find one name, and the phone has stopped ringing.
Your phone must find a contact instantly, every time anyone calls. Checking one by one takes up to 1,000,000 steps. What do you do?
- Search from the end of the list (Not this one): Maya is just as likely to be near the end as the start. On average you still check half the list: 500,000 names.
- Sort the list and use binary search (Helps for a while): Finding is fast now: about 20 checks, because each check halves what is left. But keeping the list sorted is slow: every new contact pushes up to a million names along to make room.
- Turn the name into a slot number and jump straight there (Right fix): A small calculation turns "Maya" into slot 0. You go straight to slot 0: one step, whether you have ten contacts or ten million. Hashing: A hash function turns a key, like a name, into a number. That number is the slot where the value is stored, so finding it again means doing the same calculation and looking in one place.
Step 2: How the slot is picked
A simple hash: add up the letters’ character codes…
…then take the remainder after dividing by the number of slots (8).
Ben lands in slot 5.
Chen lands in slot 6. The same name always gives the same slot, so you can always find it again.
Step 3: Two names, one slot
Now add "Lia"… slot 6 already holds Chen.
Two names land in the same slot. This is called a collision, and with enough names it always happens. What do you do?
- Overwrite Chen with Lia (Not this one): Chen is gone. Next time Chen calls, your phone says “Unknown caller”. A hash table must never lose data.
- Keep both in a small list in that slot (Right fix): Slot 6 now holds a short list: Chen, then Lia. Finding Lia means jumping to slot 6 and checking two names, not a million. Chaining: Each slot holds a small list, so a collision costs a couple of extra checks. (The other common fix, open addressing, puts the new name in the next free slot instead.)
- Make the table bigger (Helps for a while): With 16 slots, Chen moves to slot 14 and Lia gets slot 6 to herself. But add enough names and two will share a slot again, so you still need a plan for collisions.
Step 4: The table fills up
30 contacts in 8 slots: the lists get long, and lookups slow down again.
With 30 names in 8 slots, each lookup checks about 4 names. With a million names in 8 slots it would be 125,000. What do you do?
- Stop accepting new contacts (Not this one): Your users would rather have a slower phone than a full one. A hash table should grow as data grows.
- Keep adding to the lists (Helps for a while): It still works, but every name you add makes lookups slower. With enough names you are back to checking long lists one by one.
- When it is 75% full, double the slots and re-hash everyone (Right fix): With 16 slots, every name is re-hashed into its new slot and the lists shrink to about 2. Doubling again each time it fills keeps lookups short forever. Load factor and resizing: Load factor = items ÷ slots. When it passes about 0.75, the table doubles its slots and moves every item to its new slot. That move is slow but rare, so on average each add or lookup is still one step: amortised O(1).
What you learned
- Hash function: Turns a key into a slot number. The same key always gives the same slot.
- Slots (buckets): An array, so jumping to slot 6 takes one step.
- Collision: Two keys in one slot. It always happens eventually.
- Chaining: Each slot keeps a short list, so collisions cost a few checks, not a crash or lost data.
- Load factor + resize: When items ÷ slots passes ~0.75, double the slots and re-hash. Keeps the lists short.
| Operation | Unsorted list | Sorted list | Hash table |
|---|---|---|---|
| Find | O(n) | O(log n) | O(1) average |
| Add | O(1) | O(n) | O(1) average |
| Delete | O(n) | O(n) | O(1) average |
| Keeps order | Insertion order | Yes, sorted | No |
Worst case for a hash table is O(n), if every key lands in one slot. A good hash function makes that practically impossible.
In code
# Python's dict is a hash table
phone = {}
phone["Maya"] = "555-0101" # add: O(1) average
phone["Ben"] = "555-0102"
print(phone["Maya"]) # find: O(1) average
print("Chen" in phone) # False
del phone["Ben"] # delete: O(1) average- See hashing in real programs: frequency counting pattern
- Lesson: from 1 user to 1 million (system design)
- Paste your own code and watch it run
Frequently asked questions
Why is a hash table lookup O(1)?
Because finding a key means computing its slot number and looking in that one slot. The work doesn’t grow with the number of items, as long as the lists in each slot stay short.
What is a collision in a hash table?
Two different keys that hash to the same slot. Tables handle it with chaining (a small list per slot) or open addressing (use the next free slot).
What is the load factor?
The number of items divided by the number of slots. Most hash tables resize (usually doubling) when it passes about 0.75, which keeps lookups fast.
When should I not use a hash table?
When you need the keys in order, ranges like “all names from K to M”, or the smallest or largest key. A sorted structure such as a balanced tree is better for that.