Related Articles
Python | Find most frequent element in a list
• Difficulty Level : Easy
• Last Updated : 03 Apr, 2020

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.

Examples:

```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.

 `# Program to find most frequent  ` `# element in a list ` ` `  `def` `most_frequent(``List``): ` `    ``counter ``=` `0` `    ``num ``=` `List``[``0``] ` `     `  `    ``for` `i ``in` `List``: ` `        ``curr_frequency ``=` `List``.count(i) ` `        ``if``(curr_frequency> counter): ` `            ``counter ``=` `curr_frequency ` `            ``num ``=` `i ` ` `  `    ``return` `num ` ` `  `List` `=` `[``2``, ``1``, ``2``, ``2``, ``1``, ``3``] ` `print``(most_frequent(``List``)) `

Output:

```2
```

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.

 `# Program to find most frequent  ` `# element in a list ` `def` `most_frequent(``List``): ` `    ``return` `max``(``set``(``List``), key ``=` `List``.count) ` ` `  `List` `=` `[``2``, ``1``, ``2``, ``2``, ``1``, ``3``] ` `print``(most_frequent(``List``)) `

Output:

```2
```

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 `most_common()` method.

 `# Program to find most frequent  ` `# element in a list ` ` `  `from` `collections ``import` `Counter ` ` `  `def` `most_frequent(``List``): ` `    ``occurence_count ``=` `Counter(``List``) ` `    ``return` `occurence_count.most_common(``1``)[``0``][``0``] ` `   `  `List` `=` `[``2``, ``1``, ``2``, ``2``, ``1``, ``3``] ` `print``(most_frequent(``List``)) `

Output:

```2
```

Approach #4 : By finding mode

Finding most frequent element means finding mode of the list. Hence, we use mode method from statistics.

 `import` `statistics ` `from` `statistics ``import` `mode ` ` `  `def` `most_common(``List``): ` `    ``return``(mode(``List``)) ` `   `  `List` `=` `[``2``, ``1``, ``2``, ``2``, ``1``, ``3``] ` `print``(most_common(``List``)) `

Output:

```2
```

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.

 `# Program to find most frequent  ` `# element in a list ` ` `  `def` `most_frequent(``List``): ` `    ``dict` `=` `{} ` `    ``count, itm ``=` `0``, '' ` `    ``for` `item ``in` `reversed``(``List``): ` `        ``dict``[item] ``=` `dict``.get(item, ``0``) ``+` `1` `        ``if` `dict``[item] >``=` `count : ` `            ``count, itm ``=` `dict``[item], item ` `    ``return``(itm) ` ` `  `List` `=` `[``2``, ``1``, ``2``, ``2``, ``1``, ``3``] ` `print``(most_frequent(``List``)) `

Output:

```2
```

Approach #6 : Using pandas library.
Incase of multiple values getting repeated. Print all values.

 `import` `pandas as pd ` `List` `=` `[``2``, ``1``, ``2``, ``2``, ``1``, ``3``, ``1``] ` ` `  `# Create a panda DataFrame using the list ` `df``=``pd.DataFrame({``'Number'``: ``List``}) ` ` `  `# Creating a new dataframe to store the values ` `# with appropriate column name ` `# value_counts() returns the count based on ` `# the grouped column values  ` `df1 ``=` `pd.DataFrame(data``=``df[``'Number'``].value_counts(), columns``=``[[``'Number'``,``'Count'``]]) ` ` `  `# The values in the List become the index of the new dataframe. ` `# Setting these index as a column ` `df1[``'Count'``]``=``df1[``'Number'``].index ` ` `  `# Fetch the list of frequently repeated columns  ` `list``(df1[df1[``'Number'``]``=``=``df1.Number.``max``()][``'Count'``]) `

Output:

```[2,1]
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :