Skip to content

Instantly share code, notes, and snippets.

@zahash
Last active December 28, 2018 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zahash/5e0e4e0e2205f3944be03f3454597faf to your computer and use it in GitHub Desktop.
Save zahash/5e0e4e0e2205f3944be03f3454597faf to your computer and use it in GitHub Desktop.
#@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
@zahash
Copy link
Author

zahash commented Dec 28, 2018

first method to return the most frequent element of a list | array | sting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment