Data Structures19 sections · 729 units
Open in Course

How Hashing Works

The basic idea

A hash function converts a key to an array index. For key kk: index=hash(k)modtableSizeindex = hash(k) \bmod tableSize Good hash functions distribute keys uniformly.

Different keys mapping to the same index is called a collision. You don't need to implement hash functions in contests. Use the built-in: C++ has unordered_map and unordered_set, Python has dict and set.