Skip to main content

Count Elements With Maximum Frequency - LeetCode Daily Challenge

Aakash Verma

Problem Statement

You are given an array nums consisting of positive integers.

Return the total frequencies of elements in nums such that those elements all have the maximum frequency.

The frequency of an element is the number of occurrences of that element in the array.

Example 1

Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.

Example 2

Input: nums = [1,2,3,4,5]
Output: 5
Explanation: All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.

Try here before watching the video.

Video Explanation

Java Code

class Solution {
    public int maxFrequencyElements(int[] nums) {
        int[] frequency = new int[101];

        int maxFrequency = 0, count = 0;
        for(int num : nums) {
            frequency[num] += 1;
            if(frequency[num] > maxFrequency) {
                maxFrequency = frequency[num];
                count = frequency[num];
            } else if(frequency[num] == maxFrequency) {
                count += frequency[num];
            }
        }

        return count;

    }
}

C++ Code

class Solution {
public:
    int maxFrequencyElements(std::vector<int>& nums) {
        int frequency[101] = {0};

        int maxFrequency = 0, count = 0;
        for(int num : nums) {
            frequency[num]++;
            if(frequency[num] > maxFrequency) {
                maxFrequency = frequency[num];
                count = frequency[num];
            } else if(frequency[num] == maxFrequency) {
                count += frequency[num];
            }
        }

        return count;
    }
};

Python Code

class Solution:
    def maxFrequencyElements(self, nums: List[int]) -> int:
        frequency = [0] * 101
        
        maxFrequency = count = 0
        for num in nums:
            frequency[num] += 1
            if frequency[num] > maxFrequency:
                maxFrequency = frequency[num]
                count = frequency[num]
            elif frequency[num] == maxFrequency:
                count += frequency[num]
        
        return count

Javascript Code

var maxFrequencyElements = function(nums) {
    let frequency = Array(101).fill(0);
    
    let maxFrequency = 0, count = 0;
    for(let num of nums) {
        frequency[num]++;
        if(frequency[num] > maxFrequency) {
            maxFrequency = frequency[num];
            count = frequency[num];
        } else if(frequency[num] === maxFrequency) {
            count += frequency[num];
        }
    }
    
    return count;
};

Go Code

func maxFrequencyElements(nums []int) int {
    frequency := make([]int, 101)
    
    maxFrequency, count := 0, 0
    for _, num := range nums {
        frequency[num]++
        if frequency[num] > maxFrequency {
            maxFrequency = frequency[num]
            count = frequency[num]
        } else if frequency[num] == maxFrequency {
            count += frequency[num]
        }
    }
    
    return count
}

Complexity Analysis

Time Complexity: O(N), we are iterating each element once.
Space Complexity: O(N), we are taking N size extra space.