Search code examples
algorithmhashmaphashtable

How to hash key in hash table


I'm trying to implement hash table and stuck with the following. Consider that for key x and hash function h we have h(x) = 32. When size of buckets array is 10, the index of x is 2 (32 % 10). When array grows up to 40, the index of x is 32 (32 % 40). How to resolve this?


Solution

  • If you resize the hash buckets store, you'll need to iterate the old store and reassign them to the new buckets/slots based upon the new modulo of their hash to the number of buckets/slots.