Skip to content

Hash tables

A hash table uses a hash function to decide where to store an item. In this exercise, a simple modulus operation maps an ID to one of several buckets.

INTERACTIVE / LIVE

Build a hash table

Change the bucket count, add your own IDs and watch collisions appear.

Live demo

Bucket index = ID % bucket count. More than one ID in the same bucket is a collision.

A collision occurs when different keys map to the same bucket. Collisions are normal; they must be handled correctly. Common approaches include separate chaining and open addressing.

A simple illustration of a hash table with entries mapped into buckets

Operation Average case Worst case
Lookup O(1) O(n)
Insert O(1) O(n)
Delete O(1) O(n)