Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Created July 28, 2020 13:01
Show Gist options
  • Save FerdinaKusumah/c2d1bc00c9ee8b7ab5d9d69571d6c3e7 to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/c2d1bc00c9ee8b7ab5d9d69571d6c3e7 to your computer and use it in GitHub Desktop.
[Python] Most Frequent in list
from collections import Counter
"""Find most frequent in list"""
arr = [1, 2, 3, 1, 2, 4, 6, 7, 3, 2, 3, 4]
cnt = Counter(arr)
# Counter({2: 3, 3: 3, 1: 2, 4: 2, 6: 1, 7: 1})
"""Show only frequent in 2 times"""
print(cnt.most_common(2))
# [(2, 3), (3, 3)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment