Back to stories
<Engineering/>
Premium

Hash Tables in Practice: Lookups, Collisions, and When to Use Them

Share by

Hash Tables in Practice: Lookups, Collisions, and When to Use Them

Hash tables (hash maps, dictionaries, objects) give you average O(1) lookup, insert, and delete by key. They’re one of the most used data structures. This post explains how they work, how collisions are handled, and when to use them.


What a hash table does

  • Key–value storage: You associate a key with a value and can later look up, update, or delete by key.
  • Average complexity: O(1) for insert, lookup, delete. Worst case can be O(n) if many keys collide and the implementation doesn’t handle it well (e.g. poor hash or resize policy).

Under the hood: a hash function maps keys to integers; that integer is used (e.g. modulo bucket count) to pick a bucket; the key–value pair is stored in that bucket. Lookup recomputes the hash and goes to the same bucket.