
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Count Frequency of Highest Frequent Elements in Python
Suppose we have a list of numbers called nums, we have to find the most frequently present element and get the number of occurrences of that element.
So, if the input is like [1,5,8,5,6,3,2,45,7,5,8,7,1,4,6,8,9,10], then the output will be 3 as the number 5 occurs three times.
To solve this, we will follow these steps −
- max:= 0
- length:= size of nums
- for i in range 0 to length-2, do
- count:= 1
- for j in range i+1 to length-1, do
- if nums[i] is same as nums[j], then
- count := count + 1
- if nums[i] is same as nums[j], then
- if max < count, then
- max:= count
- return max
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, nums): max=0 length=len(nums) for i in range(0,length-1): count=1 for j in range(i+1,length): if(nums[i]==nums[j]): count+=1 if(max<count): max=count return max ob = Solution() nums = [1,5,8,5,6,3,2,45,7,5,8,7,1,4,6,8,9,10] print(ob.solve(nums))
Input
[1,5,8,5,6,3,2,45,7,5,8,7,1,4,6,8,9,10]
Output
3
- Related Articles
- Top K Frequent Elements in Python
- Program to find frequency of the most frequent element in Python
- List frequency of elements in Python
- Python – Fractional Frequency of elements in List
- Python – Count frequency of sublist in given list
- Find top K frequent elements from a list of tuples in Python
- Count minimum frequency elements in a linked list in C++
- Python – Restrict Elements Frequency in List
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python
- Python program to count distinct words and count frequency of them
- Python - Count the frequency of matrix row length
- Program to find highest common factor of a list of elements in Python
- Count of elements matching particular condition in Python
- Delete elements with frequency atmost K in Python
- Find sum of frequency of given elements in the list in Python

Advertisements