Skip to content

Instantly share code, notes, and snippets.

@jkariukidev
Last active Jul 7, 2020
Embed
What would you like to do?
Get a repeated number in any given array (assumption that there is one repeated number).
# Get the most repeated number in a given list
def max_freq(some_list):
"""
Gets the most repeated number in a list.
:param some_list: A given list of numbers having one that is most
repeated.
:type some_list: list
:rtype max_item: object
"""
# Most repeated item
max_item = None
# Max number of counts
max_count = -1
# Create empty dictionary to store the indexes if the list item as keys
# and the repeted count as the values.
count = {}
# Loop through the given list
for item in some_list:
if item not in count:
count[item] = 1
else:
count[item] += 1
if count[item] > max_count:
max_count = count[item]
max_item = item
return max_item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment