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.
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.
Run the simulation
Insert & lookup, watch a collision chain, then track load factor.
Put and get
Insert key→value pairs into a 5-bucket table with chaining. Watch the hash and bucket light up.
Hash table essentials
Hash · collide · rehash when crowded.
Key → index
hash(key) % m picks a bucket. A good hash spreads keys evenly.
Chain or probe
Chaining hangs entries on a list; open addressing searches another slot.
dict · set · HashMap
Python dict/set and Java HashMap/HashSet are hash tables with polished APIs.