Created
May 6, 2024 14:00
-
-
Save moriarty99779/6e85b7d0543a5f2aa41fc35bbf9c41e1 to your computer and use it in GitHub Desktop.
Python 3 Find Most Frequent Element in List
This file contains hidden or 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
from collections import Counter | |
def most_frequent(lst): | |
data = Counter(lst) | |
return data.most_common(1) # returns most frequent 1 element | |
list = [2, 1, 2, 2, 1, 3, 3, 3, 2, 3, 3, 1] | |
print(most_frequent(list)) | |
# output: [(3, 5)] # 3 is the most frequent element which appears 5 times. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment