Last active
December 28, 2018 08:37
-
-
Save zahash/5e0e4e0e2205f3944be03f3454597faf 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(arr): | |
''' In this method we will use dictionary to store the | |
count of each element and then search the dictionary | |
(or) hash map for the element with maximum count | |
this is one of the quickest methods | |
Complexity : O(n) | |
''' | |
# initialize a dictionary/hash map | |
data = {} | |
for element in arr: | |
if element not in data: | |
data[element] = 1 | |
else: | |
data[element] += 1 | |
max_value = 0 | |
for key,value in data.items(): | |
if value > max_value : | |
max_value = value | |
max_key = key | |
return max_key, max_value, data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
first method to return the most frequent element of a list | array | sting