Last active
May 13, 2018 15:16
-
-
Save keNzi/b243a498cebd79345fb21157664e89bb to your computer and use it in GitHub Desktop.
find most common number
This file contains 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
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