Two Keys in One Hash Bucket
A hash table can map distinct keys to the same bucket. Its collision-handling rule preserves the ability to distinguish them.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a simple bucket function
With index given by remainder modulo five, keys 2 and 7 both reach index two. A chained table can retain both in that bucket and compare keys during lookup. Hashable key rules explain why equal keys must agree on their hash.
Check the missing key
Searching for 12 reaches the same bucket but should find neither key. Verify the equality comparison before using a candidate's value. Dictionary lookups can report absence distinctly.[1]
Follow two equal bucket indices with different keys
A table of five buckets can use integer remainder for a toy hash index. Keys seven and twelve both go to bucket two. A chained table checks the actual entries in that bucket, so it can store both keys and select the right value. See hashable values.
Bucket 2 holds (7, amber) and (12, blue)
Find key 12: check 7, then 12 → blue
Find key 17: same bucket, neither key matches → absent
Check updates within a shared bucket
Replacing the value for twelve should preserve seven and should not grow the entry count. Removing seven should leave twelve accessible. Collisions become more costly when many keys accumulate in one place, so measure bucket occupancy rather than treating equal indices as exceptional corruption.