I want to count the number of items in a dictionary with the given value (suppose the value in dictionary is just number), I've searched online and find two approaches, the first one:
sum(x == chosen_value for x in d.values())
the second approach is using Counter in Collections module.
However, I think the running time of both approaches is O(N)
, where N
is the total number of items in the dictionary. I want to find out a way to do this in O(logN)
, is it possible?
Thanks in advance for any help and suggestion!
Update:
Thanks for all the quick reply! It cannot be done in O(logN)
. I may use binary tree to store the (key,value) pairs instead.