
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements