51

A hash table maps keys to values using a hash function that turns a key into an array index. Two keys with the same index cause a collision.

  • Chaining — each slot keeps a list of entries.
  • Open addressing — find another free slot (linear probing, quadratic probing, double hashing).
  • Load factor = items / slots; when it gets high the table is resized (rehashing).

Collision handled by chaining

Collision handled by chaining

Counting frequency with a hash map C++ 4 lines
unordered_map<string, int> freq;
for (string w : {"a", "b", "a", "c", "a"})
    freq[w]++;
cout << freq["a"];   // 3