Hash table size

By "size" of the hash table we mean how many slots or buckets it has

Choice of hash table size depends in part on choice of hash function, and collision resolution strategy

But a good general “rule of thumb” is:

The hash table should be an array with length about 1.3 times the maximum number of keys that will actually be in the table, and

Size of hash table array should be a prime number

So, let M = the next prime larger than 1.3 times the number of keys you will want to store in the table, and create the table as an array of length M

(If you underestimate the number of keys, you may have to create a larger table and rehash the entries when it gets too full; if you overestimate the number of keys, you will be wasting some space)
CONTENTS PREVIOUS NEXT