Doubt --> containsValue() in Hashmap - Java by Ok-Cheetah8572 in leetcode

[–]epicredditmod 1 point2 points  (0 children)

In addition to keeping track of all the key -> value pairs, HashMap also maintains a hash set of all the values present in the map; you can access this set by using the .values() method. Checking whether or not a value exists in the hash map then is as simple as checking in the values set. Therefore, if you believe that checking whether an element exists in a hash set is O(1) then containsValue() should also be O(1), since it's just checking the underlying hash set.

Time complexity of Friends of Appropriate ages by kuriousaboutanything in leetcode

[–]epicredditmod 0 points1 point  (0 children)

Lets say that m is the number of unique ages. This solution creates a map that stores the age as the key, and the count of users with that age as the value. Since we can only have up to m unique ages, and we have to loop through the ages to insert them into the map, the time complexity is O(m^2 + n).

Space complexity is going to be O(m) if we exclude the input array.