Total Count of Maximum Frequency Elements
Given an array of integers nums, calculate the sum of frequencies of all elements that share the maximum frequency in the array.
In other words, count how many elements (including duplicates) in nums belong to the set of elements that appear most frequently.
[ "[1, 2, 2, 3, 1, 4]" ]
Explanation. Elements 1 and 2 both appear 2 times, which is the maximum frequency. Sum of their frequencies is 2 + 2 = 4.
[ "[1, 2, 3, 4, 5]" ]
Explanation. All elements have a frequency of 1, which is the maximum frequency. Sum of frequencies is 1 + 1 + 1 + 1 + 1 = 5.
[ "[7]" ]
Explanation. The single element has a frequency of 1.
[ "[5, 5, 5, 5]" ]
Explanation. Element 5 is the only element and has a maximum frequency of 4.
[ "[10, 20, 20, 10, 30, 20, 10]" ]
Explanation. 10 appears 3 times and 20 appears 3 times, which is the maximum frequency. The sum of their counts is 3 + 3 = 6.
Follow-up: Can you compute the total count in a single pass through the array by maintaining running frequencies and tracking max count updates?
- `1 <= nums.length <= 10^5` - `1 <= nums[i] <= 1000`
- Views
- 1