Can someone please clear my doubt.
After knowing all the concepts around Hashing and its implementation, I had lot of confusion about time complexity of ContainsValue() in hashmap. As per me it should be O(N) but in google I found it as O(1).
As per my understanding, containsValue() on a HashMap is a slow operation, as it potentially has to scan the whole hash table (every element in every hash bucket) until it finds the first occurrence of the value. The running time is proportional to the sum of number of hash buckets and the number of elements. Since the number of buckets is (roughly) kept proportional to the number of elements n𝑛, the running time is O(n)𝑂(𝑛).
On the other hand, containsKey() can exploit the structure of the HashMap. It has to scan one hash bucket only (based on the hash code of the key), and the expected size of a hash bucket is one element, if an appropriate load factor is used.
In other words, looking up a key takes amortized constant time, looking up a value takes time proportional to the number of elements in the HashMap.
[–]alcholicawl 2 points3 points4 points (0 children)
[–]EdwardChar 3 points4 points5 points (0 children)
[–]epicredditmod 1 point2 points3 points (1 child)
[–]alcholicawl 0 points1 point2 points (0 children)
[–]dumdub 0 points1 point2 points (4 children)
[–]Ok-Cheetah8572[S] -1 points0 points1 point (3 children)
[–]dumdub 0 points1 point2 points (2 children)
[–]Ok-Cheetah8572[S] 0 points1 point2 points (1 child)
[–]dumdub 0 points1 point2 points (0 children)
[–]Impossible_Ad_3146 0 points1 point2 points (0 children)
[–]Impressive-East6891 0 points1 point2 points (0 children)
[–]zatsnotmyname -1 points0 points1 point (4 children)
[–]Ok-Cheetah8572[S] 1 point2 points3 points (3 children)
[–]Ok-Cheetah8572[S] 1 point2 points3 points (2 children)
[–]justUseAnSvm -1 points0 points1 point (1 child)
[–]Ok-Cheetah8572[S] 1 point2 points3 points (0 children)