开发者

What is performance of ContainsKey and TryGetValue?

开发者 https://www.devze.com 2023-03-26 04:00 出处:网络
I\'m prepping for interviews, and some obvious interview questions such as counting frequency of characters in a string involve putting all of the characters into a Hashtable/Dictionary in order to ge

I'm prepping for interviews, and some obvious interview questions such as counting frequency of characters in a string involve putting all of the characters into a Hashtable/Dictionary in order to get O(n) runtime for the algorithm. My question is, what is the performance hit by usin开发者_Go百科g ContainsKey and TryGetValue to check to see if a key has already been inserted into the Hashtable? Can I still have an O(n) algorithm for problems like these that use ContainsKey or TryGetValue?


Assuming a good hash without too many collisions, each of those are O(1) operations.

As for how those operations work... I suggest you read up on hash tables.

0

精彩评论

暂无评论...
验证码 换一张
取 消