Given a list, find the most frequent element in it. If there are multiple elements that appear maximum number of times, print any one of them.
Input : [2, 1, 2, 2, 1, 3] Output : 2 Input : ['Dog', 'Cat', 'Dog'] Output : Dog
Approach #1 : Naive Appraoch
This is a brute force approach in which we make use of for loop to count the frequency of each element. If the current frequency is greater than the previous frequency, update the counter and store the element.
Approach #2 : Pythonic Naive appraoch
Make a set of the list so that the duplicate elements are deleted. Then find the highest count of occurrences of each element in the set and thus, we find the maximum out of it.
Approach #3 : Using Counter
Make use of Python Counter which returns count of each element in the list. Thus, we simply find the most common element by using
Approach #4 : By finding mode
Finding most frequent element means finding mode of the list. Hence, we use mode method from statistics.
Approach #5 : Using Python dictionary
Use python dictionary to save element as a key and its frequency as the value, and thus find the most frequent element.
- Python | Find top K frequent elements from a list of tuples
- Python | Find most common element in a 2D list
- Python | Find most common element in each column in a 2D list
- Find the k most frequent words from data set in Python
- Python | Find the tuples containing the given element from a list of tuples
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- Python | Every Kth element in list
- Python | How to get the last element of list
- Python | Three element sum in list
- Python | Add the element in the list with help of indexing
- Python | Insert after every Nth element in a list
- Python | Operate on every Kth element of list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.