Problem

You are given an array nums consisting of positive integers.

Return thetotal frequencies of elements in __nums such that those elements all have themaximum frequency.

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

Examples

Example 1

1
2
3
4
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

1
2
3
4
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.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Solution

Method 1 – Hash Map Frequency Counting

Intuition

We count the frequency of each element, find the maximum frequency, and sum the counts of all elements that have this maximum frequency.

Approach

  1. Use a hash map to count the frequency of each element in the array.
  2. Find the maximum frequency among all elements.
  3. Sum the frequencies of all elements that have this maximum frequency.
  4. Return the sum.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
public:
    int maxFrequencyElements(vector<int>& nums) {
        unordered_map<int, int> cnt;
        int mx = 0, ans = 0;
        for (int x : nums) cnt[x]++;
        for (auto& [k, v] : cnt) mx = max(mx, v);
        for (auto& [k, v] : cnt) if (v == mx) ans += v;
        return ans;
    }
};
1
2
3
4
5
6
7
8
func maxFrequencyElements(nums []int) int {
    cnt := map[int]int{}
    mx, ans := 0, 0
    for _, x := range nums { cnt[x]++ }
    for _, v := range cnt { if v > mx { mx = v } }
    for _, v := range cnt { if v == mx { ans += v } }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
    public int maxFrequencyElements(int[] nums) {
        Map<Integer, Integer> cnt = new HashMap<>();
        int mx = 0, ans = 0;
        for (int x : nums) cnt.put(x, cnt.getOrDefault(x, 0) + 1);
        for (int v : cnt.values()) mx = Math.max(mx, v);
        for (int v : cnt.values()) if (v == mx) ans += v;
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    fun maxFrequencyElements(nums: IntArray): Int {
        val cnt = mutableMapOf<Int, Int>()
        var mx = 0
        var ans = 0
        for (x in nums) cnt[x] = cnt.getOrDefault(x, 0) + 1
        for (v in cnt.values) mx = maxOf(mx, v)
        for (v in cnt.values) if (v == mx) ans += v
        return ans
    }
}
1
2
3
4
5
6
class Solution:
    def maxFrequencyElements(self, nums: list[int]) -> int:
        from collections import Counter
        cnt = Counter(nums)
        mx = max(cnt.values())
        return sum(v for v in cnt.values() if v == mx)
1
2
3
4
5
6
7
8
9
use std::collections::HashMap;
impl Solution {
    pub fn max_frequency_elements(nums: Vec<i32>) -> i32 {
        let mut cnt = HashMap::new();
        for &x in &nums { *cnt.entry(x).or_insert(0) += 1; }
        let mx = *cnt.values().max().unwrap();
        cnt.values().filter(|&&v| v == mx).map(|&v| v).sum()
    }
}
1
2
3
4
5
6
7
8
class Solution {
    maxFrequencyElements(nums: number[]): number {
        const cnt: Record<number, number> = {};
        for (const x of nums) cnt[x] = (cnt[x] || 0) + 1;
        const mx = Math.max(...Object.values(cnt));
        return Object.values(cnt).filter(v => v === mx).reduce((a, b) => a + b, 0);
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the array, for counting and summing.
  • 🧺 Space complexity: O(n), for storing the frequency map.