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

后端 未结 8 1080
独厮守ぢ
独厮守ぢ 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-14 00:02

    from the docs:

    Retrieving a value by using its key is very fast, close to O(1), because the T:System.Collections.Generic.Dictionary`2 class is implemented as a hash table.

    So it can be O(1) but might be slower. Here you can find another thread regarding hashtable performance: Hash table - why is it faster than arrays?

提交回复
热议问题