HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Hash table collisions: why use a linked list if we can use a hash set?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
whycantablesethashlistuselinkedcollisions

Problem

One way to deal with the problem of collisions for a hash table is to have a linked list for each bucket. But then the lookup time is no longer constant. Why not use a hash set instead of a linked list? The lookup time would then be constant. For example, in C++ the hash table could be defined as:

unordered_map> m;

Solution

An hash set is an hash table. Using an hash set to handle collisions in an hash table is equivalent to using a bigger hash table, with an hashing function which is a combination of the hashing functions of both level.

In other words, you'd probably be better with a bigger initial table (for instance there is no risk of resonance between the two hash functions which could lead to an higher collision rate than expected at the second level).

Context

StackExchange Computer Science Q#51230, answer score: 8

Revisions (0)

No revisions yet.