Hash Tables Explained Simply

Your phone finds “Lia” among 800,000 contacts before you finish typing. It doesn’t look through them one by one. It turns the name into a number that says exactly where Lia is stored, and jumps straight there. That trick is a hash table.

Data structures · 2026-11-25 · 6 min read

The problem: searching a list is slow

If names sit in a plain list, finding one means checking them in order. With 800,000 contacts that can be 800,000 checks: O(n) time, which grows with the data. Sorting helps (binary search takes about 20 checks), but every new contact then has to be squeezed into its sorted place.

The idea: compute where it lives

A hash table keeps an array of buckets (slots). To store a key, it runs the key through a hash function that turns it into a number, then takes that number modulo the number of buckets to get a slot:

slot = hash("Lia") % 8      # e.g. 2034167 % 8 = 7
table[7] = ("Lia", "+1 555 0142")

Looking Lia up runs the same maths and goes straight to slot 7. No matter how many contacts there are, that is a handful of steps: O(1) on average.

Collisions

There are far more possible keys than buckets, so two keys will sometimes land in the same slot, say “Lia” and “Chen” both on slot 7. That is a collision, and every hash table must handle it:

  • Chaining: each bucket holds a short list; colliding keys are added to it. Java’s HashMap does this.
  • Open addressing: if the slot is taken, try the next free one by a fixed rule. Python’s dict does this.

Either way, a lookup still checks only one or two items, as long as buckets don’t get crowded.

Load factor and resizing

The load factor is items ÷ buckets. As it climbs, collisions pile up and lookups slow down. So when it passes a limit (around 0.75 in Java, about 2/3 in Python), the table doubles its buckets and re-inserts every key into its new slot. One resize is slow, but it happens rarely, so adds stay O(1) on average.

OperationAverageWorst case
Look up a keyO(1)O(n) if everything collides
InsertO(1)O(n) during a resize
DeleteO(1)O(n)
Visit keys in sorted orderNot supportedSort first: O(n log n)

Hash tables in your language

# Python
phone = {"Lia": "555-0142"}
phone["Chen"] = "555-0199"
print(phone.get("Lia"))

// Java
Map<String, String> phone = new HashMap<>();
phone.put("Lia", "555-0142");
phone.get("Lia");

Python’s dict and set, Java’s HashMap and HashSet, JavaScript’s Map and objects, Go’s map and C#’s Dictionary are all hash tables.

When not to use one

  • You need keys in order, or ranges like “all names from K to N”: use a sorted structure such as a balanced binary search tree.
  • You need the smallest or largest item fast: use a heap.
  • The data is tiny: a plain list is just as fast and simpler.

Lesson: binary search trees →

See it happen

In the interactive lesson a contacts app slows to a crawl. You pick the fix each time: hashing, handling a collision, then resizing a crowded table, and watch the data structure change on screen.

Play: hash tables, step by step →

Frequently asked questions

Why is a hash table lookup O(1)?

Because the hash function computes the slot directly from the key, so the lookup goes straight to one bucket instead of searching. With a good hash and a sensible load factor, each bucket holds only one or two items.

What is a hash collision?

Two different keys mapping to the same slot. Tables handle it with chaining (a small list per slot) or open addressing (probing for another free slot).

What is the load factor?

The number of stored items divided by the number of buckets. When it passes a threshold, around 0.75, the table grows and re-inserts its keys to keep lookups fast.

Is a Python dict a hash table?

Yes. Python’s dict and set are hash tables using open addressing. Since Python 3.7 a dict also remembers insertion order.

Keep reading

← All guides

Try AI Flowchart free →