Skip to content

Instantly share code, notes, and snippets.

@jkariscodes
Last active July 7, 2020 12:41
Show Gist options
  • Save jkariscodes/1398ea88dd4fdf1d0340ab0960370511 to your computer and use it in GitHub Desktop.
Save jkariscodes/1398ea88dd4fdf1d0340ab0960370511 to your computer and use it in GitHub Desktop.
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