Cover image for LeetCode Daily Problem P3005 Maximum Frequency Element Count

LeetCode Daily Problem P3005 Maximum Frequency Element Count


Timeline

Timeline

2025-09-22

init

Counting Sort

Problem:

This problem mainly uses array indices as identifiers for elements, and array elements record frequencies.
First traversal records the frequency of all elements, second traversal finds the maximum frequency, third traversal sums all elements with that maximum frequency. Overall complexity O(n).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <limits.h>
#include <string.h>
#include <vector>
using std::vector;

class Solution {
public:
// 1 <= nums[i] <= 100
int maxFrequencyElements(vector<int> &nums)
{
int n = nums.size();
int max = INT_MIN;
int res = 0;
int array[101];
memset(array, 0, 101 * sizeof(int));
for (int i = 0; i < n; i++) {
array[nums[i]]++;
}

for (int i = 1; i < 101; i++) {
if (array[i] > max) {
max = array[i];
}
}
for (int i = 1; i < 101; i++) {
if (array[i] == max) {
res += array[i];
}
}
return res;
}
};