Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Element with largest frequency in list

Python | Element with largest frequency in list

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




# 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

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




# 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

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




# 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

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




# 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

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 5:  using operator.countOf() method

Python3




# Python3 code to demonstrate
# to get most frequent element
# using naive method
import operator as op
# 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 = op.countOf(test_list, 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

Time Complexity: O(N)
Auxiliary Space : O(1)

Method 5:  Using numpy.bincount()

Python3




# Python3 code to demonstrate
# to get most frequent element
# using numpy.bincount()
import numpy as np
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
 
# printing original list
print("Original list : " + str(test_list))
 
print(np.bincount(test_list).argmax())
#This code is contributed by Vinay Pinjala.


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]
 
# creating an empty dictionary
freq_dict = {}
 
# iterating over the list
for element in test_list:
    # checking if the element already exists in the dictionary
    if element in freq_dict:
        # if yes, incrementing the count
        freq_dict[element] += 1
    else:
        # if not, adding the element to the dictionary
        # and initializing its count as 1
        freq_dict[element] = 1
 
# finding the most frequent element
most_frequent_element = max(freq_dict, key=freq_dict.get)
 
# printing original list
print("Original list : " + str(test_list))
# printing the most frequent element
print("The most frequent element is:", most_frequent_element)
#this code is contributed by tvsk


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




# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
 
# printing original list
print ("Original list : " + str(test_list))
 
# using list comprehension to get most frequent element
res = max(set(test_list), key = lambda x: test_list.count(x))
 
# printing result
print ("Most frequent number is : " + str(res))
#this code is contributed by Vinay Pinjala.


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

  1. Import the numpy library.
  2. Initialize a numpy array from the given list using numpy.array() method.
  3. Use numpy.unique() method to find the unique elements of the array and their counts.
  4. Get the index of the maximum count value using numpy.argmax() method.
  5. Use the index obtained to get the element with the highest frequency.

Python3




# Python3 code to demonstrate
# to get most frequent element
# using numpy
 
import numpy as np
 
# initializing list
test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
 
# printing original list
print("Original list : " + str(test_list))
 
# using numpy to get most frequent element
arr = np.array(test_list)
unique, counts = np.unique(arr, return_counts=True)
res = unique[np.argmax(counts)]
 
# 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

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.

RELATED ARTICLES

Most Popular

Recent Comments