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.
1 <= nums.length <= 1001 <= nums[i] <= 100Use a HashMap to count frequencies, then find maximum frequency and sum all elements with that frequency.
Algorithm:
getOrDefaultExample Input: nums = [1, 2, 2, 3, 1, 4]
Step-by-step execution:
Process nums[5] = 4: map = {1: 2, 2: 2, 3: 1, 4: 1}
Result: 4
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;
    }
}
Use a fixed-size frequency array since nums[i] ≤ 100, then track maximum frequency while counting.
Algorithm:
Example Input: nums = [1, 2, 2, 3, 1, 4]
Step-by-step execution:
Process nums[5] = 4: freqArr[4] = 1, maxFreq = 2
Result: 4
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;
    }
}
| 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: