What is the time complexity of checking if an element is present in a hash table?
-
For example, consider this sample Python program to find the frequency of the letters: string = "aaabbbabababcccddcdcdcaacbbbcbbdddeffeeaa" def freq(string): hashmap = {} for x in string: if x in hashmap: hashmap[x] += 1 else: hashmap[x] = 1 return hashmap print freq(string) You just have to traverse the string once, but does the fact that you have to check if every element is in the hashmap add to the time complexity? So is the complexity [math] O(n) [/math] or more?
-
Answer:
This thread contains some nice explanations: http://cs.stackexchange.com/q/249/906
Franck Dernoncourt at Quora Visit the source
Other answers
For chaining : Expected: O(1) or to be more precise O(1+a) for both successful search and unsuccessful search; Worst Case: Can be O(n) For Probing: Expected: O(1/(1-a)) for successful search O( (1/a) ln( 1/(1-a) ) ) for unsuccessful search where a is load factor of table. There also exists some other hashing techniques with different complexities. These two are most common.
Himanshu Bhardwaj
Related Q & A:
- How to find the time complexity?Best solution by Stack Overflow
- What is Hash Table, how to create a hash table?Best solution by Stack Overflow
- How to do reverse lookup in a Hash?Best solution by reverse-hash-lookup.online-domain-tools.com
- What's a hash check?Best solution by Yahoo! Answers
- What are the differences between people who have dyslexia, and people who have a hard time writing papers?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.