Skip to content

Instantly share code, notes, and snippets.

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