Skip to content

Commit a66f4db

Browse files
authored
Create 3005. Count Elements With Maximum Frequency 1 (#892)
2 parents 65e0797 + a74b44f commit a66f4db

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int maxFrequencyElements(vector<int>& nums) {
9+
unordered_map<int,int> freq;
10+
// Count frequency of each number
11+
for (int x : nums) freq[x]++;
12+
13+
// Find maximum frequency
14+
int maxFreq = 0;
15+
for (auto &p : freq) maxFreq = max(maxFreq, p.second);
16+
17+
// Sum frequencies of elements that have frequency == maxFreq
18+
int result = 0;
19+
for (auto &p : freq) if (p.second == maxFreq) result += p.second;
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)