Skip to content
StackDecode
SearchingAnimated

Hash-based Search

Jump straight to a bucket computed from the key.

Time

O(1) avg, O(n) worst

Extra space

O(n)

Use it for

Exact-match lookups with no ordering needs.

How it works

  1. Store each value in a bucket chosen by a hash function: bucket = value mod m.
  2. To search, compute the same hash of the target.
  3. Only look inside that one bucket (values that collide share a bucket).
  4. With a good hash and enough buckets, lookup takes O(1) on average.

Watch it run

Loading animation…