Skip to content
Related Articles

Related Articles

Improve Article
Python | Element with largest frequency in list
  • Last Updated : 02 Dec, 2018

This is one of the most essential operation that programmer quite often comes in terms with. Be it development or competitive programming, this utility is quite essential to master as it helps to perform many tasks that involve this task to be its subtask. Lets discuss various approaches to achieve this operation.

Method #1 : Naive method
As the brute force method, we just count all the elements and then just return the element whole count remains the maximum at the end. This is the basic method that one could think of executing when faced with this issue.




# Python3 code to demonstrate 
# to get most frequent element
# using naive method
  
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
  
# printing original list
print ("Original list : " + str(test_list))
  
# using naive method to 
# get most frequent element
max = 0
res = test_list[0]
for i in test_list:
    freq = test_list.count(i)
    if freq > max:
        max = freq
        res = i
      
# printing result
print ("Most frequent number is : " + str(res))

Output :

Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4

Method #2 : Using max() + set()
Converting the list to set and maximizing the function with respect to the count of each number in the list, this task can be achieved with ease and is most elegant way to achieve this.




# Python3 code to demonstrate 
# to get most frequent element
# using max() + set()
  
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
  
# printing original list
print ("Original list : " + str(test_list))
  
# using max() + set() to 
# get most frequent element
res = max(set(test_list), key = test_list.count)
      
# printing result
print ("Most frequent number is : " + str(res))

Output :



Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4

Method #3 : Using statistics.mode()
Mode denoted the maximum frequency element in mathematics and python dedicates a whole library to statistical function and this can also be used to achieve this task.




# Python3 code to demonstrate 
# to get most frequent element
# using statistics.mode()
import statistics
  
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
  
# printing original list
print ("Original list : " + str(test_list))
  
# using statistics.mode() to 
# get most frequent element
res = statistics.mode(test_list)
      
# printing result
print ("Most frequent number is : " + str(res))

Output :

Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4

Method #4 : Using collections.Counter.most_common()
The lesser known method to achieve this particular task, Counter() uses the most_common function to achieve this in one line. This is innovative and different way to achieve this.




# Python3 code to demonstrate 
# to get most frequent element
# using collections.Counter.most_common()
from collections import Counter
  
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
  
# printing original list
print ("Original list : " + str(test_list))
  
# using most_common to 
# get most frequent element
test_list = Counter(test_list)
res = test_list.most_common(1)[0][0]
      
# printing result
print ("Most frequent number is : " + str(res))

Output :

Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :