How do you find the maximum number of repeated elements in a list in Python?

Python | Find most frequent element in a list

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 Approach
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 approach
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]

How do you find the maximum number of repeated elements in a list in Python?




Article Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list

“how to count max repeated count in list python” Code Answer


python find most occuring element
python by Arno Deceuninck on Jul 07 2020 Comment
4
Source: stackoverflow.com
Add a Grepper Answer

  • how to check if there are duplicates in a list python
  • count number of repeats in list python
  • count similar values in list python
  • generate a list of random non repeated numbers python
  • max of double array python
  • python count repeated elements in a list
  • program to print duplicates from a list of integers in python
  • count the duplicates in a list in python
  • count different values in list python
  • how to find duplicate numbers in list in python
  • how to find most repeated word in a string in python
  • count repeat values in python list
  • find the item with the maximum number of occurrences in a list in Python
  • how do you count most frequent item in a list in python
  • list of single item repeated python
  • calculate the same value in list i python

  • most frequent digit in list in python
  • python list 10 most frequent elemens in list
  • how to get most common value in list python
  • find the most repeated instance in array python
  • python get most frequent item in list
  • how to find the most common word in a list 'python'
  • python most common element in list
  • python most common elements in list
  • cout how many time most commoin item in list apperaes
  • find most occuring element in list python
  • python get most occurent array
  • most frequent number is a list python
  • most frequent word in a list python
  • how to find most occuring number in a list python without library
  • most common in list python
  • highest occuring of a number in list
  • most occurring number python
  • how to find what number appears most in list python
  • maximum occurance of an element in a list
  • python search list and output most commen
  • most common in python
  • most occurred element in list python
  • count the most occuring number in the list
  • find most return value in python
  • print highest number of times in python
  • print maximum occurring element in a list pythin
  • python list get most frequent element
  • most repeated element in an array python
  • print the most repeated element in python
  • most frequent element in an python array
  • how to find the most frequent element in a list python
  • get most frequent value in list python
  • most common element inlist
  • python test if item is most frequent in list
  • python find most commmon label
  • python the element occured the most in a list
  • how to check how which element occurs the most in python list
  • python print the most string occurrences in list
  • find number most occuring python
  • most occuring number python
  • python find most occuring element in a list
  • python most occuring element in list python
  • most occuring number in the list python
  • how to find the most occurences of something in a list
  • function to find number with most occurences in list python
  • how to find which element occurs most in a list python
  • python most occurences in list
  • most occuring value in list python
  • program to identify most occuring number in a list
  • most frequent number in list python
  • python get element with most occurences in list
  • the most concurring number in list python
  • find max repeating element in array python
  • find highest occuring element in list
  • how to print the maximum count
  • highest repeating value in array python
  • how to find the value that specified the most times in a list
  • find most common item in lsit python
  • most recurring item in list python
  • most three frequent number is a list python
  • python select most frequent element in list
  • python get element used most frequent
  • python most frequent number in list
  • return most common element in list python
  • function that detects most recurring values in list
  • find maximum occurrence of a number in an array python
  • get the most frequent value in a list python
  • python get the most frequent works
  • most recurring number in a list
  • most occouring in list
  • find element which appears most in list
  • how to find most frequent number in list python
  • most common number in python
  • c# find most common item in array python
  • python remove the most frequent element in list
  • how to find higgest occering number in two list
  • python find most repeated item in list
  • python find the most common element in a list
  • python find most repeat in text
  • how to get most occuring element in python
  • python find most common element in list
  • find most common element in list python
  • how to print maximum count element in list python
  • python find most occurrence in list
  • most frequent element in list python
  • find most occurring element in array python
  • write a program that will go through a list to see which value occurs the most.
  • python all most common element in list
  • import counter and find most frequent python
  • most common method python
  • returns the number that occured most in a list python
  • python find element with most occurrence in list
  • python list most frequent element
  • python find most frequent string in list
  • find most present variables python
  • highest occurrences in python
  • you have a list of integers, return the x most frequent element of that list
  • python get most occurring element in list
  • most frequent element in array python without imports
  • get most occuring value in a list in python
  • how to find the most common element in a list python
  • python find item with most digits in list
  • find the most repeated number in an list in python
  • find most common value in list python
  • pythom get a list and return the most common index
  • how to find the most frequent values in a list python
  • how to get the most frequent element in a list python
  • max count in list python
  • most frequent values in a list
  • check which value has the most occurences in python list
  • how to get all max occuring element in python
  • python list elemnt the most
  • python get most occuring number
  • python element with max count
  • given an array find the most frequent number python
  • function for most occuring element in a list in python
  • python find most occuring item in list
  • python3 get most occuring item in list
  • how to get element that occur the most in a list python
  • find element with most occurrence in list
  • get most occuring elements in a list
  • how to check for the most occured element in a list python
  • find top 50 most occuring in list python
  • get the most occuring number in a list pythno
  • python 3 most occuring element in list
  • how to get the most occuring value in a list python
  • how to find most occuring number in a list python
  • most occured element in the list
  • python get most occurrences in a list
  • max occuring value list python
  • find most common in list python
  • find frequency of most frequent element in array python without import
  • find most frequent element in list python
  • how to find how many times the most occuring element occurs python
  • find most frequent element in a list python
  • function which return most common element of a list
  • python how to find the most common specific elemnt in a file
  • python find most common item
  • how to get max occured in python in list
  • top 10 most repeated items in a list python
  • return element with maximum count in list python
  • find the most common number in aa list
  • python find count max value in list
  • python max number occurrences in list
  • find most frequent item in a list
  • find the most common number in array python
  • find element with maximum frequency in list python using dictionary
  • count the number that appears the most in a list python
  • mhow to print most common at index
  • most occurring string in list
  • most common list pyhton
  • find most occurances pytnon
  • python find most common multiple
  • find most common element in python using pandas
  • python program to show the most frequent in each column
  • maximum repeated element in an array in python
  • get most common value in list python
  • python most frequent element in list
  • print which element occurs most in list python
  • how to count max repeated count in list python
  • find most frequent element in an array python
  • python get most common element in list
  • python most common element in list online test
  • find th emost common welement in a array python
  • python most frequently occuring string in a list
  • how to find which data is appearing maximum number of times python
  • max repeated element in list python
  • find most common number in list python
  • most common element in a list python
  • how to get the most common number in python
  • find the most occurring element in a list python
  • find most occuring element in string python
  • python how to find the most common elemnt in a file
  • in python to get the most repeated element from alist
  • return most occuring string in pythoon
  • print the most recurring element in a list
  • five most commonly occurring numbers python
  • print value taht occur most times in array python
  • most frequent element in array python
  • find max occuring element in tuple python
  • highest number of times word python list
  • most frequent python
  • how to find most occurring item in list python
  • python list top 10 freqent elements
  • print the most frequency counter python
  • python return most common item in list
  • most frequent element in an array python
  • how we can find those numbers which occured the most frequent time in a list
  • most common value in list python
  • counter() print out most common element python
  • most common element on many list python
  • most frequently occured nymber inn a list program in python
  • find 2 most frequent element in list python
  • piython most occurence in list
  • get most frequent value in a list in python
  • python get value with most occurrences
  • how to find most occuring item in array in python
  • python find the most occuring element in a list
  • find items in list that occur the most python
  • get the most occuring element in a list python
  • python get most occuring element in list
  • find most occurence in list python
  • get the number of most occurrences in a list python
  • python most occuring element in list
  • calculate most occuring value of a list in python
  • find element that occurs most times in array python
  • most occurrence element in list python
  • print the most occuring element python
  • most common value function python
  • most of number list python
  • most common item in list python
  • python most common value
  • find the most common number in a list
  • find frequency of most frequent element in array python without imports
  • how to find the value that's specified the most times in a list
  • most common element in array python
  • look for the most frecuent number in a python list
  • how to grab most common item in list python
  • counter most frequent python
  • python count max occurrences in list
  • python most frequent elements in list
  • python most common value in array
  • python count of max number occurrences in list
  • how to return string that is occurring the most in the list python
  • find the most frequent number in array python
  • list maximum repeated elements python
  • python find the item in array that apears the most
  • choosing highest occuring elements in python
  • most occuring string in list
  • how to find most repeated element in python
  • frequency of most frequent digit in list in python
  • how to find most common element in a list python
  • mostfrequency + python
  • how to find max number of occurrences in list python
  • python which number appears the most
  • find frequent number in array python
  • how to find maximum repect number in list python
  • how i find the most shot values in a list

Find most frequent element in a list in Python

PythonServer Side ProgrammingProgramming

In this article we will see how to find the element which is most common in a given list. In other words, the element with highest frequency.

Find Maximum Value in List in Python

In this article, we will learn to find the maximum value in a list in Python. We will use some built-in functions, simple approaches, and some custom codes as well to understand the logic. Let's first have a quick look over what is a list in Python and how can we find the maximum value or largest number in a list.