leetcode

Count Elements With Maximum Frequency - Link

Question Description

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.

In other words, return the sum of frequencies of all elements that appear with the highest frequency.


Constraints


Approaches

Approach 1: HashMap-Based Approach

Use a HashMap to count frequencies, then find maximum frequency and sum all elements with that frequency.

Algorithm:

  1. Create a HashMap to store frequency of each number
  2. Iterate through array and count frequencies using getOrDefault
  3. Find the maximum frequency by iterating through map values
  4. Sum all frequencies that equal the maximum frequency
  5. Return the total count

Dry Run (HashMap)

Example Input: nums = [1, 2, 2, 3, 1, 4]

Step-by-step execution:

Result: 4


Solution (HashMap)

class Solution {
    public int maxFrequencyElements(int[] nums) {
        HashMap<Integer, Integer> freq = new HashMap<>();

        // Step 1: count frequencies
        for (int num : nums) {
            freq.put(num, freq.getOrDefault(num, 0) + 1);
        }

        // Step 2: find maximum frequency
        int max = 0;
        for (int val : freq.values()) {
            max = Math.max(max, val);
        }

        // Step 3: sum up frequencies of elements with maximum frequency
        int ans = 0;
        for (int val : freq.values()) {
            if (val == max) ans += val;
        }

        return ans;
    }
}

Time and Space Complexity (HashMap)


Approach 2: Frequency Array (More Optimal)

Use a fixed-size frequency array since nums[i] ≤ 100, then track maximum frequency while counting.

Algorithm:

  1. Create frequency array of size 101 (since nums[i] ≤ 100)
  2. Count frequencies and track maximum frequency simultaneously
  3. Sum all frequencies that equal the maximum frequency
  4. Return the total count

Dry Run (Frequency Array)

Example Input: nums = [1, 2, 2, 3, 1, 4]

Step-by-step execution:

Result: 4


Solution (Frequency Array)

class SolutionArray {
    public int maxFrequencyElements(int[] nums) {
        int[] freqArr = new int[101];
        int maxFreq = 0;

        // Step 1: count frequencies and track max
        for (int num : nums) {
            freqArr[num]++;
            maxFreq = Math.max(maxFreq, freqArr[num]);
        }

        // Step 2: sum up contributions of max frequency
        int total = 0;
        for (int freq : freqArr) {
            if (freq == maxFreq) {
                total += freq;
            }
        }

        return total;
    }
}

Time and Space Complexity (Frequency Array)


Comparison of Approaches

Approach Time Complexity Space Complexity When to Use
HashMap O(n) O(k) General case, unknown range
Frequency Array O(n) O(1) Small known range (1-100)

Key Insight: Since the range of numbers is small (1-100), the frequency array approach is more efficient and uses constant space.

Why Sum Frequencies?

Edge Cases:

Alternative Mathematical Approaches:

  1. Two-pass algorithm: First pass to find max frequency, second pass to sum
  2. Single-pass with tracking: Track max frequency and sum simultaneously
  3. Sorting approach: Sort and count consecutive elements