What are ContainsKey and TryGetValue?

I'm getting ready for an interview, and some obvious interview questions, such as counting the frequency of characters in a string, include placing all the characters in a Hashtable / Dictionary to get O (n) runtime for the algorithm. My question is: what is the efficiency using ContainsKey and TryGetValue to check if a key has already been inserted into a Hashtable? Can I still have an O (n) algorithm for such problems that use ContainsKey or TryGetValue ?

+8
c # algorithm big-o
source share
1 answer

Assuming a good hash without too many collisions, each of them is O (1) operation.

As for how these operations work ... I suggest you read the hash tables .

+9
source share

All Articles