Skip to content

Commit b72d874

Browse files
authored
Create 3005. Count Elements With Maximum Frequency (#424)
2 parents 27d3467 + 0a9b83b commit b72d874

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
// time/space: O(n)/O(n)
4+
int maxFrequencyElements(vector<int>& nums) {
5+
// count the frequency for each integer
6+
unordered_map<int, int> freq;
7+
for (auto& num : nums) freq[num]++;
8+
9+
// find the maximum frequency
10+
int maxFreq = 0;
11+
for (auto& [num, f] : freq) maxFreq = max(maxFreq, f);
12+
13+
// calculate the sum of the frequencies with `maxFreq`
14+
int result = 0;
15+
for (auto& [num, f] : freq) {
16+
if (f == maxFreq) result += f;
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)