Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How stupid it is to use an integer value a key for an Hash Table?

I'll be needing to use Hash Tables with different keys. One as string for the key and another as integer.

As for the integer one, how stupid it is to run a hash function on the number to generate a key?

I mean, the numbers I'll be using as key for the Hash Table will always be different, there will be no duplicates at all. Isn't enough for me to use the mod operator to "truncate" the value below the hash table size?

Or there's something more to it?

like image 746
rfgamaral Avatar asked Jan 22 '23 11:01

rfgamaral


1 Answers

It is fine unless there's a high chance that your integer keys are 62, 93, 124, ... and your hash table size happens to be 31.

See What integer hash function are good that accepts an integer hash key? if you worry about this.

like image 175
kennytm Avatar answered Feb 14 '23 09:02

kennytm