Hash Table (Linear Probing)
Map keys to slots with simple arithmetic.
A hash table stores key-value pairs by computing an index from the key using a hash function (key mod tableSize). When two keys hash to the same slot (a collision), linear probing searches for the next empty slot by checking index+1, index+2, and so on.
h(22) = 22 mod 7 = 1
[0] [1] [2] [3] [4] [5] [6]
22
h(15) = 15 mod 7 = 1 ← collision!
try 2, try 3... insert at 2
[0] [1] [2] [3] [4] [5] [6]
22 15How it works
- 1Compute h(key) = key mod tableSize
- 2If the slot is empty, insert there
- 3If occupied (collision), try the next slot: (h+1), (h+2), ...
- 4Repeat until an empty slot is found
Loading...