Element with Largest Frequency in List (original) (raw)
Last Updated : 30 Jan, 2025
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.Counter
Counter is a convenient way to count elements and get the most common one directly.
Python `
from collections import Counter
a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] c = Counter(a) f = c.most_common(1)[0][0] # Get the element with highest frequency
print(f)
`
**Explanation:
- Counter(a) creates a dictionary-like object that counts the frequency of each element in the list a.
- most_common(1) method returns most frequent element as a list of tuples and [0][0] extracts the element itself. In this case output is 4 element with highest frequency.
Using max()
with count()
We can use max()
with count() to find the element with highest frequency by checking the count of each element.
Python `
a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] m = max(set(a), key=a.count) # Find element with maximum count
print(m)
`
**Explanation:
set(a)
converts the lista
into a set removing duplicates so only unique elements remain.max(..., key=a.count)
function finds element in the set with the highest count in the original lista
and in this case it returns element with the most occurrences.
**Using a Dictionary to Count Occurrences
We can manually count occurrences of each element and then find the one with largest count.
Python `
a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] f = {}
for num in a: f[num] = f.get(num, 0) + 1
m = max(f, key=f.get) # Get the key with max value
print(m)
`
**Explanation:
for
loop iterates through lista
andf.
get(num, 0) + 1
expression counts occurrences of each number storing them in dictionaryf
.max(f, key=f.get)
finds key in dictionaryf
with highest value and it returns4
element with highest count.
**Using sorted()
We can sort list based on frequency and then pick the element with highest frequency.
Python `
a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
Sort unique elements based on their frequency in descending order
m = sorted(set(a), key=lambda x: a.count(x), reverse=True)[0]
print(m)
`
**Explanation:
- set
(a)
extracts unique elements froma
and sorted(..., key=lambda x: a.count(x), reverse=True)
sorts them in descending order based on their frequency ina
. [0]
retrieves the first element from the sorted list which is the one with the highest frequency in this case4
Similar Reads
- Find element with highest frequency in given nested Array Given an array arr[] of N integers. The task is to create a frequency array freq[] of the given array arr[] and find the maximum element of the frequency array. If two elements have the same frequency in the array freq[], then return the element which has a smaller value. Examples: Input: arr[] = {1 8 min read
- Largest subarray with frequency of all elements same Given an array arr[] of N integers, the task is to find the size of the largest subarray with frequency of all elements the same. Examples: Input: arr[] = {1, 2, 2, 5, 6, 5, 6} Output: 6 Explanation: The subarray = {2, 2, 5, 6, 5, 6} has frequency of every element is 2. Input: arr[] = {1, 1, 1, 1, 1 15+ min read
- Find the element in a linked list with frequency at least N/3 Given a linked list of size N consisting of a string as node value, the task is to find the majority string, having frequency greater than [N/3], in the linked list. Note: It is guaranteed that there is only one majority string. Examples: Input: head -> geeks -> geeks -> abcd -> game - 11 min read
- Count minimum frequency elements in a linked list Given a linked list containing duplicate elements. The task is to find the count of all minimum occurring elements in the given linked list. That is the count of all such elements whose frequency is minimum in the matrix. Examples: Input : 1-> 2-> 2-> 3 Output : 2 Explanation: 1 and 3 are e 8 min read
- Find array elements with frequencies in range [l , r] Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai 9 min read
- Design a stack with max frequency element operation Given n elements and the task is to implement a stack which removes and returns the maximum frequency element on every pop operation. If there's a tie in the frequency then the topmost highest frequency element will be returned.Examples: Input: push(4) 8 push(6) 6 push(7) 7 push(6) 6 push(8); 4Outpu 11 min read
- Least frequent element in an array Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them. Examples : Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1}Output : 3Explanation: 3 appears minimum number of times in given array. Input : arr[] = {10, 20, 30}Outp 11 min read
- Maximum element in an array which is equal to its frequency Given an array of integers arr[] of size N, the task is to find the maximum element in the array whose frequency equals to it's value Examples: Input: arr[] = {3, 2, 2, 3, 4, 3} Output: 3 Frequency of element 2 is 2 Frequency of element 3 is 3 Frequency of element 4 is 1 2 and 3 are elements which h 11 min read
- Sum of all maximum frequency elements in Matrix Given a NxM matrix of integers containing duplicate elements. The task is to find the sum of all maximum occurring elements in the given matrix. That is the sum of all such elements whose frequency is even in the matrix. Examples: Input : mat[] = {{1, 1, 1}, {2, 3, 3}, {4, 5, 3}} Output : 12 The max 6 min read
- Find the frequency of each element in a sorted array Given a sorted array, arr[] consisting of N integers, the task is to find the frequencies of each array element. Examples: Input: arr[] = {1, 1, 1, 2, 3, 3, 5, 5, 8, 8, 8, 9, 9, 10} Output: Frequency of 1 is: 3 Frequency of 2 is: 1 Frequency of 3 is: 2 Frequency of 5 is: 2 Frequency of 8 is: 3 Frequ 10 min read