Why is accessing an element of a dictionary by key O(1) even though the hash function may not be O(1)?

后端 未结 8 994
独厮守ぢ
独厮守ぢ 2020-12-13 23:53

I see how you can access your collection by key. However, the hash function itself has a lot of operations behind the scenes, doesn\'t it?

Assuming you have a nice h

8条回答
  •  囚心锁ツ
    2020-12-13 23:57

    Theoretically it is still O(n), because in the worst case all your data may end up having identical hash and be bundled together in which case you have to linearly go through all of it.

提交回复
热议问题