Skip to content

Instantly share code, notes, and snippets.

@zahash
Created December 28, 2018 08:33
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/7fe88d0432c8249162681fb82d3c22b5 to your computer and use it in GitHub Desktop.
Save zahash/7fe88d0432c8249162681fb82d3c22b5 to your computer and use it in GitHub Desktop.
#@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
@zahash
Copy link
Author

zahash commented Dec 28, 2018

second method to find the most frequent element in the list | string | tuple

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