Hash tables

A hash function turns a key into a bucket index. Collisions share a chain; load factor tracks how full the table is. This is how Python dict / set and Java HashMap / HashSet work.

Data structures Intermediate Hashing · Collisions ~9 min
The idea

Key → bucket → entry

Arrays give O(1) access by index. A hash table invents an index from the key: bucket = hash(key) % m. Fast lookup, insert, and delete when the hash spreads keys well.

When two keys land in the same bucket, we resolve the collision — here with chaining (a short list per bucket). The load factor n / m says how crowded the table is; when it grows too high, implementations rehash into a larger array.

TipSets store keys only; dictionaries store key→value. Same machinery underneath.
IdeaHash then place
hash_insert.pyPython
m = 5                 # bucket count
bucket = hash(key) % m
table[bucket].append((key, val))

Demo uses a tiny table (m = 5) so collisions are easy to see.

Interactive

Run the simulation

Insert & lookup, watch a collision chain, then track load factor.

Step 0 / 0
demo.pyPython
Bucketsdemo
Ready
Shortcuts: Space play/pause · → step · ← back · R reset
Try it yourself

Put and get

Insert key→value pairs into a 5-bucket table with chaining. Watch the hash and bucket light up.

Ready
Empty table (m = 5). Enter a key and value, then Put.
Remember

Hash table essentials

Hash · collide · rehash when crowded.

Hash

Key → index

hash(key) % m picks a bucket. A good hash spreads keys evenly.

Collisions

Chain or probe

Chaining hangs entries on a list; open addressing searches another slot.

Languages

dict · set · HashMap

Python dict/set and Java HashMap/HashSet are hash tables with polished APIs.

Related

Sets as an ADT

Use the ADT view — uniqueness and set algebra — then come back to see the engine.

Open sets