How does a hash map work?

Why lookups are instant: a hash function turns any key straight into a bucket index.

A hash map turns a key into a number with a hash function, and that number says exactly which bucket the value lives in. No scanning, no searching: compute, jump, done.

Two keys can land in the same bucket (a collision), so buckets hold small lists. As long as collisions stay rare, lookups stay effectively O(1), which is why hash maps sit under almost every language's dictionary.

Remember this

  • hash(key) → bucket index: lookup without searching
  • Collisions are handled inside the bucket
  • Average O(1) get and put

Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.

Ask your own question →

Keep learning