Python | Element with largest frequency in list
Last Updated :
22 Apr, 2023
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
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
max = 0
res = test_list[ 0 ]
for i in test_list:
freq = test_list.count(i)
if freq > max :
max = freq
res = i
print ( "Most frequent number is : " + str (res))
|
Output
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(n2), O(n) for the loop, and O(n) for count method
Auxiliary Space: O(1)
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
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
res = max ( set (test_list), key = test_list.count)
print ( "Most frequent number is : " + str (res))
|
Output
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
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
import statistics
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
res = statistics.mode(test_list)
print ( "Most frequent number is : " + str (res))
|
Output
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
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
from collections import Counter
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
test_list = Counter(test_list)
res = test_list.most_common( 1 )[ 0 ][ 0 ]
print ( "Most frequent number is : " + str (res))
|
Output
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5: using operator.countOf() method
Python3
import operator as op
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
max = 0
res = test_list[ 0 ]
for i in test_list:
freq = op.countOf(test_list, i)
if freq> max :
max = freq
res = i
print ( "Most frequent number is : " + str (res))
|
Output
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(N)
Auxiliary Space : O(1)
Method 5: Using numpy.bincount()
Python3
import numpy as np
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
print (np.bincount(test_list).argmax())
|
Output:
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(N)
Auxiliary Space : O(1)
Method 6: Using dictionary.
Python3
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
freq_dict = {}
for element in test_list:
if element in freq_dict:
freq_dict[element] + = 1
else :
freq_dict[element] = 1
most_frequent_element = max (freq_dict, key = freq_dict.get)
print ( "Original list : " + str (test_list))
print ( "The most frequent element is:" , most_frequent_element)
|
Output
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
The most frequent element is: 4
Time Complexity: O(N)
Auxiliary Space : O(N)
Method 7: Using list comprehension
Python3
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
res = max ( set (test_list), key = lambda x: test_list.count(x))
print ( "Most frequent number is : " + str (res))
|
Output
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(N)
Auxiliary Space : O(N)
Method 8: using numpy.argmax() method
- Import the numpy library.
- Initialize a numpy array from the given list using numpy.array() method.
- Use numpy.unique() method to find the unique elements of the array and their counts.
- Get the index of the maximum count value using numpy.argmax() method.
- Use the index obtained to get the element with the highest frequency.
Python3
import numpy as np
test_list = [ 9 , 4 , 5 , 4 , 4 , 5 , 9 , 5 , 4 ]
print ( "Original list : " + str (test_list))
arr = np.array(test_list)
unique, counts = np.unique(arr, return_counts = True )
res = unique[np.argmax(counts)]
print ( "Most frequent number is : " + str (res))
|
Output:
Original list : [9, 4, 5, 4, 4, 5, 9, 5, 4]
Most frequent number is : 4
Time Complexity: O(nlogn), O(nlogn) for sorting unique array in numpy, and O(1) for getting the highest frequency value.
Auxiliary Space: O(n), as we are storing the counts of each element in the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...