Hashing
Turn keys into array positions for near-constant-time lookup, and handle collisions.
- Hash functions: from key to bucket index A hash function turns a key into a bucket index. See keys map to buckets and how a good function spreads them evenly.
- Hash tables with separate chaining With separate chaining, each bucket holds a small list of the keys that hash there. Insert, search and delete walk one short chain.
- Open addressing: linear, quadratic probing, double hashing Open addressing stores every key in the bucket array itself and probes for the next free slot. Compare linear, quadratic and double-hash…
- Load factor and resizing The load factor is how full a hash table is. When it passes a threshold the table doubles and every key moves to a new bucket.
- Hash sets and maps in practice: counting, lookup, deduplication Hash sets and maps answer "have I seen this?" in O(1). Use them to count, look up, group and deduplicate, as in Two Sum.
- Collisions, worst cases and mutable keys Bad keys can pile into one bucket and turn a hash table into a slow list. Mutable keys can get lost after they change.