Mode
given a list of integers,write a program to print the mode.
mode - the most common value in the list. if multiple elements with same frequency are present, print all values with same frequency in increasing order.
input
the input will be a single line of containing space-separated integers.
output
the third line of output should contain the mode.
see sample input/output for the output format
explanation
for example, if the given list of integers are
2 4 5 6 7 8 2 4 5 2 3 8
the average of all the numbers is 4.67.
after sorting the array,
2 2 2 3 4 4 5 6 7 8 8
as 2 is most repeated number , the mode will be 2.
so the output should be
Mode: 2
sample input 1
2 4 5 6 7 8 2 4 5 2 3 8
sample output 1
Mode: 2
from collections import Counter
arr = input(">> ").split(" ")
c = Counter(arr)
print("Mode: " + c.most_common(1)[0][0])
Comments
Leave a comment