HashSet look-up complexity?

前端 未结 3 388
南笙
南笙 2020-12-10 00:40

A look-up operation OR contains for single can be O(n) in worst-case right ? So, for n elements look up in hashSet will b

相关标签:
3条回答
  • 2020-12-10 01:21

    Yes, but it's really the worst case: if all the elements in the HashSet have the same hash code (or a hash code leading to the same bucket). With a correctly written hashCode and a normally distributed key sample, a lookup is O(1).

    0 讨论(0)
  • 2020-12-10 01:25

    Yes, but the whole reason we have HashSets is that we encounter this worst case with very, very low probability, and it's usually much faster than the guaranteed nlogn for a heap or a (self-balancing) TreeSet, or the guaranteed n^2 for an unsorted list.

    0 讨论(0)
  • 2020-12-10 01:29

    lookp takes O(c)

    c = constant value

    0 讨论(0)
提交回复
热议问题