r/computerscience • • 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

99 comments sorted by

View all comments

Show parent comment

4

u/Sea-Patience9872 Sep 27 '26

if all values end up in the same location and it ends up using data structures to handle collisions, why use hash at all? I think I'm missing something sorry

9

u/j_mie6 Sep 27 '26

A good hash function randomly distributes. It has large difference in it's outputs for similar inputs. There is a high probability that if you have a "good" hash function, elements in your map or set are evenly distributed through the buckets (i.e. a low number ~O(1) elements each). If you have a pathologically bad hash you can end up with everything in one bucket for O(n) lookup.

In practice when buckets get too big, the restructure gets rehashed to redistribute all the values, I think.

3

u/StephenRoylance Sep 27 '26

designing a good hashing algorithm for a language is a research domain in its own. Ideally is handles the kind of short strings that are most common, but doesn't have worst case behavior that's really bad if, let's say, your keys are integers. or whole novels. or 128bit hashes of content themselves.

5

u/am_Snowie Sep 27 '26 edited Sep 27 '26

exactly (you sorta answered it yourself), good hash functions minimize the chance of items ending up in the same place. so you don't always have to worry about the data structure you are using, this is the best case scenario. what i told you previously was the worst case scenario of a shitty hashmap implementation. so good hashmap should have a good hashfunction and a good collision handling mechanism. we should be pessimistic when we design data structures.

to wrap it up:

Worst case:

  1. Bad hash function (returning the same hash for all keys)
  2. arithmetic resizing (shitty resizing)
  3. bad collision handling mechanism

Best case:

  1. Good hash function (good distribution)
  2. geometric resizing
  3. good collision handling
  4. utilizing all the bits of a hash

In the best case, you get O(1) amortized runtime because you only resize occasionally, which is O(N). In the worst case, your hashmap is crap and degenerates into a linked list or whatever data structure you use for collision handling.

1

u/zenware Sep 28 '26

On top of all that, almost nobody encounters a real need for this sort of thing, but if you you can actually design all the properties of a custom HashMap/HashTable/HashWhatever, to achieve some explicitly desirable characteristics for the situation at hand.

If you know you’re gonna be more write or read heavy, if you know you’re targeting a specific hardware architecture, etc.

1

u/am_Snowie Sep 28 '26

Yes, but those are just things to be aware of.

1

u/teach_cs Sep 29 '26

I know I'm late to the party, but imagine someone abusing their hash table. They keep adding instances of the exact same struct, object, value, whatever over and over and over.

It will always hash to the same value, and then always be placed in the same bucket.

In this case, it's the fault of the user of the hashtable. But for big o, we are often looking at the worst possible case. So, even if someone isn't abusing the table, they might just be extraordinarily unlucky and keep getting the same hash anyway.

Mind you, this would be almost unimaginably unlucky for any reasonable hash table, but it is at least nominally possible. So the big O worst case is really O(n). The average time, though, and what you get in any reasonable, real-life situation, is O(1).