Python | Find frequency of largest element in list
Last Updated :
21 Mar, 2023
Given a list, the task is to find the number of occurrences of the largest element of the list.
Examples:
Input : [1, 2, 8, 5, 8, 7, 8]
Output :3
Input : [2, 9, 1, 3, 4, 5]
Output :1
Method 1: The naive approach is to find the largest element present in the list using max(list) function, then iterating through the list using a for loop and find the frequency of the largest element in the list. Below is the implementation.
Python3
# Python program to find the
# frequency of largest element
L = [1, 2, 8, 5, 8, 7, 8]
# print the frequency of largest element
frequency = print(L.count(max(L)))
Output:
3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using collections.Counter()
Once initialized, counters are accessed just like dictionaries. Also, it does not raise the KeyValue error (if key is not present) instead the value’s count is shown as 0.
Python3
# Python program to find the
# frequency of largest element
import collections
L = [1, 2, 8, 5, 8, 7, 8]
# find the largest element
largest = max(L)
# Storing the occurrences of each
# element of list in res
res = collections.Counter(L)
print(res[largest])
Output:
3
Time Complexity: O(n) where n is the number of elements in the list.
Auxiliary Space: O(1) constant additional space is created.
Method 3: Using the dictionary
In this approach, the number of occurrences of each element is stored in a dictionary as a key-value pair, where key is the element and value is the frequency.
Python3
# Python program to find the
# frequency of largest element
L = [1, 2, 8, 5, 8, 7, 8]
d= {}
# find the largest element
largest = max(L)
for i in L:
if i in d:
d[i] += 1
else:
d[i] = 1
print(d[largest])
Output:
3
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Element with Largest Frequency in List We are given a list we need to find the element with largest frequency in a list . For example, we are having a list a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to find the element with most frequency which will be 4 in this case.Using collections.CounterCounter is a convenient way to count elements
3 min read
Python Program to Find Largest Element in an Array To find the largest element in an array, iterate over each element and compare it with the current largest element. If an element is greater, update the largest element. At the end of the iteration, the largest element will be found. Given an array, find the largest element in it. Input : arr[] = {1
4 min read
Find the k most frequent words from data set in Python The goal is to find the k most common words in a given dataset of text. We'll look at different ways to identify and return the top k words based on their frequency, using Python.Using collections.Countercollections.Counter that works like a dictionary, but its main job is to count how many times ea
3 min read
Python heapq.nlargest() Method The heapq.nlargest() method in Python is a useful function from the heapq module that returns the n largest elements from an iterable, such as a list or tuple. This method is particularly handy when we want to quickly find the largest elements from a dataset, using a heap-based approach.Basic Exampl
4 min read
Get Second Largest Number in Python List Using Bubble Sort Finding the second-largest number in a list is a common programming task that involves sorting the list in ascending order. Bubble sort is a simple sorting algorithm that can be used for this purpose. In this article, we will explore the logic behind finding the second largest number using bubble so
3 min read