Finding Things Instantly
A dictionary (map) ADT stores key–value pairs for fast lookup, insertion, and deletion by key. The underlying structure is the hash table.
A hash function computes an integer from a key. Reduced modulo the array size, this gives the index of the bucket holding that value:
When keys spread evenly, operations take on average. Because two different keys can yield the same index, collisions occur. Every hash table uses a collision-resolution strategy to prevent overwriting values.
| Strategy | Mechanism |
|---|---|
| Chaining | Stores a small list at each bucket for all matching keys |
| Open addressing | Probes to a different empty bucket by a fixed rule |
Performance and Pitfalls
Performance relies on the load factor (elements divided by buckets). As the table fills, collisions increase. To maintain speed, hash tables resize by allocating a larger array and rehashing all elements.
Common Pitfall: Believing hash lookups are guaranteed . The is an average under a good hash function and low load factor.
| Case | Complexity | Condition |
|---|---|---|
| Average case | Good hash function, reasonable load factor | |
| Worst case | Many keys colliding into a single bucket |