Count Elements With Maximum Frequency - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,3,2,3,3]
Output: 3
💡 Note: Element 3 appears 3 times (maximum frequency). Elements 1 and 2 appear 1 time each. Total frequency of elements with maximum frequency = 3.
Example 2 — Multiple Elements with Same Max Frequency
$ Input: nums = [1,1,2,2,3]
Output: 4
💡 Note: Elements 1 and 2 both appear 2 times (maximum frequency). Element 3 appears 1 time. Total frequency of elements with maximum frequency = 2 + 2 = 4.
Example 3 — All Elements Same Frequency
$ Input: nums = [1,2,3]
Output: 3
💡 Note: All elements appear 1 time (maximum frequency). Total frequency = 1 + 1 + 1 = 3.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Count Elements With Maximum FrequencyStep 1: Input Array13233Step 2: Frequency Map1 appears 1 time2 appears 1 time3 appears 3 times (MAX)Step 3: ResultMaximum frequency = 3Elements with max freq:Element 3 (freq: 3)Total = 3Sum frequencies of elements with maximum frequency
Understanding the Visualization
1
Input Array
Array of positive integers with varying frequencies
2
Count Frequencies
Build frequency map and find maximum frequency
3
Sum Maximum Frequencies
Add up frequencies of elements with maximum count
Key Takeaway
🎯 Key Insight: Track maximum frequency dynamically while building frequency map for optimal O(n) solution
Asked in
Amazon 25 Google 18 Microsoft 15
12.5K Views
Medium Frequency
~8 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen