Hash tables
Introduction / Definition
A hash table maps keys to array positions using a hash function. It aims for fast average-case lookup, insertion, and deletion. Different keys can collide at the same index. Chaining and open addressing are common collision strategies.
Syntax / Structure / Formula
index = hash(key) mod tableSize
Detailed Explanation
A good hash function distributes keys well. Load factor affects collisions and performance; resizing can restore efficiency.
Example
Example: hash(key)=123 and table size=10 gives index 3.
Output / Result
The key is assigned to bucket 3 unless collision handling redirects it.
Important Points / Notes
- Average lookup is commonly O(1).
- Worst case can be O(n).
- Load factor matters.
Mini Practice Question
Why do collisions occur?
CheckpointTest Yourself