Created
December 28, 2018 08:33
-
-
Save zahash/7fe88d0432c8249162681fb82d3c22b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@time_it | |
def dict_method_v2(arr): | |
''' In this method we will remove some of the redundancies | |
of the dict_method | |
please note that this method takes a bit more time | |
because we are making more comparisions for the max_value | |
than before | |
Complexity : O(n) | |
''' | |
# initialize a dictionary/hash map | |
data = {} | |
max_value = 0 | |
for element in arr: | |
if element not in data: | |
data[element] = 1 | |
else: | |
data[element] += 1 | |
if data[element] > max_value: | |
max_value = data[element] | |
max_key = element | |
return max_key, max_value, data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
second method to find the most frequent element in the list | string | tuple