LeetCode Problem Workspace
Count Elements With Maximum Frequency
Count Elements With Maximum Frequency is solved by counting each value, tracking the highest count, and summing matching frequencies.
3
Topics
8
Code langs
3
Related
Practice Focus
Easy · Array scanning plus hash lookup
Answer-first summary
Count Elements With Maximum Frequency is solved by counting each value, tracking the highest count, and summing matching frequencies.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array scanning plus hash lookup
For Count Elements With Maximum Frequency, the key move is to count how many times each number appears, then identify the largest frequency. After that, add the frequencies of every value that reaches that maximum. In nums = [1,2,2,3,1,4], both 1 and 2 appear twice, so the total count contributed by maximum-frequency elements is 4.
Problem Statement
You are given an array of positive integers called nums. Your goal is not to return which values appear most often, but the total number of array positions occupied by values whose frequency is tied for the highest frequency in the array.
That means you first need the occurrence count for each distinct number, then find the maximum of those counts, and finally sum every frequency equal to that maximum. For nums = [1,2,2,3,1,4], the values 1 and 2 both occur 2 times, so the answer is 2 + 2 = 4.
Examples
Example 1
Input: nums = [1,2,2,3,1,4]
Output: 4
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
Input: nums = [1,2,3,4,5]
Output: 5
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 Approach
Count every value with a hash map
Scan nums once and store frequency counts by value. This fits the Array plus Hash Table pattern exactly because the only information that matters is how many times each number appears, not where it appears.
Find the maximum frequency
After building the counts, inspect the frequency values and record the largest one. In this problem, the maximum frequency is the threshold that decides which elements contribute to the final total.
Sum frequencies that match the maximum
Loop through the frequency map again and add each count equal to the maximum frequency. A common mistake is counting how many values hit the maximum instead of summing their frequencies, which gives the wrong result on cases like [1,2,2,3,1,4].
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(n) |
The runtime is O(n) because you scan the array to build frequencies and then scan the counted values to find and sum the maximum-frequency entries. The space usage is O(n) in the general hash map version because distinct values may each need their own counter, even though this specific problem has small value bounds.
What Interviewers Usually Probe
- The interviewer mentions finding frequencies of all elements, which strongly points to a counting map before any final calculation.
- If they ask why the answer for [1,2,2,3,1,4] is 4 instead of 2, they are testing whether you sum frequencies rather than count distinct winners.
- If they emphasize Easy plus Array and Hash Table, they usually want a direct counting pass, not sorting or nested scans.
Common Pitfalls or Variants
Common pitfalls
- Returning the number of elements with maximum frequency instead of the total of their frequencies.
- Tracking the maximum frequency correctly but forgetting to do a second pass to add all tied frequencies.
- Using repeated scans for each number, which works on tiny inputs but misses the intended hash lookup pattern.
Follow-up variants
- Return the actual values whose frequencies are tied for the maximum instead of the summed frequency total.
- Update the answer for a stream of numbers where frequencies change after each insertion.
- Solve the same task with a fixed-size counting array because nums[i] is bounded by 100.
FAQ
What is the main pattern for Count Elements With Maximum Frequency?
The core pattern is array scanning plus hash lookup. You count each number, find the largest frequency, then sum all counts equal to that largest frequency.
Why is the answer 4 for nums = [1,2,2,3,1,4]?
The numbers 1 and 2 each appear 2 times, and 2 is the maximum frequency in the array. Since the problem asks for the total frequencies of all elements tied at that maximum, the result is 2 + 2 = 4.
Can I sort the array instead of using a hash map?
Yes, but it is not the cleanest match for this problem. Sorting adds extra work, while a frequency map directly models the requirement to count occurrences and compare them.
Why is counting distinct winners wrong here?
Because the problem asks for the total frequencies, not the number of values tied at the top. In the first example there are 2 winning values, but their total frequency is 4, which is the required answer.
Is O(n) time really enough for LeetCode 3005?
Yes. One pass can build counts, and a small follow-up pass over those counts can find the maximum and sum matching frequencies, which stays linear in the input size.
Solution
Solution 1: Counting
We can use a hash table or array $cnt$ to record the occurrence of each element.
class Solution:
def maxFrequencyElements(self, nums: List[int]) -> int:
cnt = Counter(nums)
mx = max(cnt.values())
return sum(x for x in cnt.values() if x == mx)Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array scanning plus hash lookup
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward