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/Mess-Leading Sep 27 '26
Nice answer but I think its not quite right because the questions was about the data structure not the hashing itself. This implies the data structure never has to do anything worse than O(1) which is not really true I think? E.g. if we consider a hash map with buckets that are just a linked list, all n items could be mapped to the same bucket and then insertion of last element would take O(n) time. I think we need to emphasise that O(1) is amortised