LLevelUp
0
← Back to topic

Hash Tables

A hash table maps keys to values by running each key through a hash function to pick a bucket. Done well, lookups, inserts, and deletes are all O(1) on average.

function hash(key: string, buckets: number): number {
  let h = 0;
  for (const ch of key) h = (h * 31 + ch.charCodeAt(0)) % buckets;
  return h;
}

WARNING

Two different keys can hash to the same bucket — a collision. Every real hash table needs a strategy for them (chaining with linked lists, or open addressing). Ignore collisions and your map silently loses data.

CAUTION

As the load factor (entries ÷ buckets) climbs past ~0.7, collisions pile up and O(1) quietly degrades toward O(n). Production tables resize to keep it low.