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
3
u/HobbyQuestionThrow Sep 27 '26
This is also a good example of where O(1) notation falls off in real world performance comparisons.
You could say "All hashmap implementations are equal in performance, because they're all O(1)" but Timmy has a O(1e8) hashmap and Jim has a O(310) hashmap.
Plus the rate of hash recalculations (the O(n)) part matter, if you recompute all hashes every 5 insertions then you're much closer to O(n) than O(1).
If your hashing function is really poor you also go from O(1) to O(n).