Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@keNzi
Last active May 13, 2018 15:16
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 keNzi/b243a498cebd79345fb21157664e89bb to your computer and use it in GitHub Desktop.
Save keNzi/b243a498cebd79345fb21157664e89bb to your computer and use it in GitHub Desktop.
find most common number
numbers = [1, 2, 3, 2, 2, 2, 2, 2, 9, 1]
def find_most_common_number(numbers):
from collections import Counter
return print(Counter(numbers).most_common(1)[0][0])
def find_most_common_number(numbers):
count_list = [[x, numbers.count(x)] for x in set(numbers)]
z, m = count_list[0]
for x, y in count_list:
if y > m:
z, m = x, y
return print(z)
find_most_common_number(numbers)
def find_most_common_number(numbers):
d = {}
for x in numbers:
if x in d:
d[x] += 1
else:
d[x] = 1
count = 0
result = None
for k, v in d.items():
if v > count:
count, result = v, k
return print(result)
find_most_common_number(numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment