Last active
July 7, 2020 12:41
-
-
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).
This file contains hidden or 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
# 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