Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashtable/Dictionary collisions

Using the standard English letters and underscore only, how many characters can be used at a maximum without causing a potential collision in a hashtable/dictionary.

So strings like:

blur
Blur
b
Blur_The_Shades_Slightly_With_A_Tint_Of_Blue

...

like image 278
Joan Venge Avatar asked Nov 30 '22 07:11

Joan Venge


1 Answers

There's no guarantee that you won't get a collision between single letters.

You probably won't, but the algorithm used in string.GetHashCode isn't specified, and could change. (In particular it changed between .NET 1.1 and .NET 2.0, which burned people who assumed it wouldn't change.)

Note that hash code collisions won't stop well-designed hashtables from working - you should still be able to get the right values out, it'll just potentially need to check more than one key using equality if they've got the same hash code.

Any dictionary which relies on hash codes being unique is missing important information about hash codes, IMO :) (Unless it's operating under very specific conditions where it absolutely knows they'll be unique, i.e. it's using a perfect hash function.)

like image 118
Jon Skeet Avatar answered Dec 05 '22 05:12

Jon Skeet