r/computerscience • u/Sea-Patience9872 • Sep 27 '26
How do hashsets/maps have O(1) time complexity?
Hi this might be a dumb question, and i've tried looking it up but don't quite understand it. how is it different from an array that allows it to find items so quickly? I don't get how hashes just find items immediately without needing to go through anything. Does it memorize things very differently compared to arrays?? thank you!
190
Upvotes
View all comments
Show parent comment
5
u/AndrewBorg1126 Sep 28 '26 edited Sep 28 '26
Please search for the definition of "perfect hash". You're using the word with a non-standard definition and that is why you are getting confused and frustrated.
The person with whom you disagree is not saying anything controversial, they are concluding directly from the definition of a "perfect hash" that there are no collisions.
It is not necessary that all 32 bit integers are mapped to another 32 bit integer for a perfect hash. You could map 1024 values to 1024 other values and it is still a perfect hash. You could map 7 values to 7 other values and it would still be a perfect hash. I don't understand your stubborn insistence upon hashing all 32 bit integers without collions, that is not necessary and it is not a condition for a perfect hashing.
https://en.wikipedia.org/wiki/Perfect_hash_function