Skip to content

Commit 33756e5

Browse files
authored
feat: add solutions to lc problem: No.3005 (doocs#2216)
No.3005.Count Elements With Maximum Frequency
1 parent 8c4f972 commit 33756e5

File tree

7 files changed

+243
-6
lines changed

7 files changed

+243
-6
lines changed

solution/3000-3099/3005.Count Elements With Maximum Frequency/README.md

+85-3
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,116 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:计数**
49+
50+
我们可以用一个哈希表或数组 $cnt$ 记录每个元素出现的次数。
51+
52+
然后我们遍历 $cnt$,找到出现次数最多的元素,记其出现次数为 $mx$,累加出现次数等于 $mx$ 的元素的出现次数,即为答案。
53+
54+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
55+
4856
<!-- tabs:start -->
4957

5058
### **Python3**
5159

5260
<!-- 这里可写当前语言的特殊实现逻辑 -->
5361

5462
```python
55-
63+
class Solution:
64+
def maxFrequencyElements(self, nums: List[int]) -> int:
65+
cnt = Counter(nums)
66+
mx = max(cnt.values())
67+
return sum(x for x in cnt.values() if x == mx)
5668
```
5769

5870
### **Java**
5971

6072
<!-- 这里可写当前语言的特殊实现逻辑 -->
6173

6274
```java
63-
75+
class Solution {
76+
public int maxFrequencyElements(int[] nums) {
77+
int[] cnt = new int[101];
78+
for (int x : nums) {
79+
++cnt[x];
80+
}
81+
int ans = 0, mx = -1;
82+
for (int x : cnt) {
83+
if (mx < x) {
84+
mx = x;
85+
ans = x;
86+
} else if (mx == x) {
87+
ans += x;
88+
}
89+
}
90+
return ans;
91+
}
92+
}
6493
```
6594

6695
### **C++**
6796

6897
```cpp
69-
98+
class Solution {
99+
public:
100+
int maxFrequencyElements(vector<int>& nums) {
101+
int cnt[101]{};
102+
for (int x : nums) {
103+
++cnt[x];
104+
}
105+
int ans = 0, mx = -1;
106+
for (int x : cnt) {
107+
if (mx < x) {
108+
mx = x;
109+
ans = x;
110+
} else if (mx == x) {
111+
ans += x;
112+
}
113+
}
114+
return ans;
115+
}
116+
};
70117
```
71118
72119
### **Go**
73120
74121
```go
122+
func maxFrequencyElements(nums []int) (ans int) {
123+
cnt := [101]int{}
124+
for _, x := range nums {
125+
cnt[x]++
126+
}
127+
mx := -1
128+
for _, x := range cnt {
129+
if mx < x {
130+
mx, ans = x, x
131+
} else if mx == x {
132+
ans += x
133+
}
134+
}
135+
return
136+
}
137+
```
75138

139+
### **TypeScript**
140+
141+
```ts
142+
function maxFrequencyElements(nums: number[]): number {
143+
const cnt: number[] = Array(101).fill(0);
144+
for (const x of nums) {
145+
++cnt[x];
146+
}
147+
let [ans, mx] = [0, -1];
148+
for (const x of cnt) {
149+
if (mx < x) {
150+
mx = x;
151+
ans = x;
152+
} else if (mx === x) {
153+
ans += x;
154+
}
155+
}
156+
return ans;
157+
}
76158
```
77159

78160
### **...**

solution/3000-3099/3005.Count Elements With Maximum Frequency/README_EN.md

+85-3
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,112 @@ So the number of elements in the array with maximum frequency is 5.
3939

4040
## Solutions
4141

42+
**Solution 1: Counting**
43+
44+
We can use a hash table or array $cnt$ to record the occurrence of each element.
45+
46+
Then we traverse $cnt$ to find the element with the most occurrences, and let its occurrence be $mx$. We sum up the occurrences of elements that appear $mx$ times, which is the answer.
47+
48+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
49+
4250
<!-- tabs:start -->
4351

4452
### **Python3**
4553

4654
```python
47-
55+
class Solution:
56+
def maxFrequencyElements(self, nums: List[int]) -> int:
57+
cnt = Counter(nums)
58+
mx = max(cnt.values())
59+
return sum(x for x in cnt.values() if x == mx)
4860
```
4961

5062
### **Java**
5163

5264
```java
53-
65+
class Solution {
66+
public int maxFrequencyElements(int[] nums) {
67+
int[] cnt = new int[101];
68+
for (int x : nums) {
69+
++cnt[x];
70+
}
71+
int ans = 0, mx = -1;
72+
for (int x : cnt) {
73+
if (mx < x) {
74+
mx = x;
75+
ans = x;
76+
} else if (mx == x) {
77+
ans += x;
78+
}
79+
}
80+
return ans;
81+
}
82+
}
5483
```
5584

5685
### **C++**
5786

5887
```cpp
59-
88+
class Solution {
89+
public:
90+
int maxFrequencyElements(vector<int>& nums) {
91+
int cnt[101]{};
92+
for (int x : nums) {
93+
++cnt[x];
94+
}
95+
int ans = 0, mx = -1;
96+
for (int x : cnt) {
97+
if (mx < x) {
98+
mx = x;
99+
ans = x;
100+
} else if (mx == x) {
101+
ans += x;
102+
}
103+
}
104+
return ans;
105+
}
106+
};
60107
```
61108
62109
### **Go**
63110
64111
```go
112+
func maxFrequencyElements(nums []int) (ans int) {
113+
cnt := [101]int{}
114+
for _, x := range nums {
115+
cnt[x]++
116+
}
117+
mx := -1
118+
for _, x := range cnt {
119+
if mx < x {
120+
mx, ans = x, x
121+
} else if mx == x {
122+
ans += x
123+
}
124+
}
125+
return
126+
}
127+
```
65128

129+
### **TypeScript**
130+
131+
```ts
132+
function maxFrequencyElements(nums: number[]): number {
133+
const cnt: number[] = Array(101).fill(0);
134+
for (const x of nums) {
135+
++cnt[x];
136+
}
137+
let [ans, mx] = [0, -1];
138+
for (const x of cnt) {
139+
if (mx < x) {
140+
mx = x;
141+
ans = x;
142+
} else if (mx === x) {
143+
ans += x;
144+
}
145+
}
146+
return ans;
147+
}
66148
```
67149

68150
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxFrequencyElements(vector<int>& nums) {
4+
int cnt[101]{};
5+
for (int x : nums) {
6+
++cnt[x];
7+
}
8+
int ans = 0, mx = -1;
9+
for (int x : cnt) {
10+
if (mx < x) {
11+
mx = x;
12+
ans = x;
13+
} else if (mx == x) {
14+
ans += x;
15+
}
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func maxFrequencyElements(nums []int) (ans int) {
2+
cnt := [101]int{}
3+
for _, x := range nums {
4+
cnt[x]++
5+
}
6+
mx := -1
7+
for _, x := range cnt {
8+
if mx < x {
9+
mx, ans = x, x
10+
} else if mx == x {
11+
ans += x
12+
}
13+
}
14+
return
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxFrequencyElements(int[] nums) {
3+
int[] cnt = new int[101];
4+
for (int x : nums) {
5+
++cnt[x];
6+
}
7+
int ans = 0, mx = -1;
8+
for (int x : cnt) {
9+
if (mx < x) {
10+
mx = x;
11+
ans = x;
12+
} else if (mx == x) {
13+
ans += x;
14+
}
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def maxFrequencyElements(self, nums: List[int]) -> int:
3+
cnt = Counter(nums)
4+
mx = max(cnt.values())
5+
return sum(x for x in cnt.values() if x == mx)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxFrequencyElements(nums: number[]): number {
2+
const cnt: number[] = Array(101).fill(0);
3+
for (const x of nums) {
4+
++cnt[x];
5+
}
6+
let [ans, mx] = [0, -1];
7+
for (const x of cnt) {
8+
if (mx < x) {
9+
mx = x;
10+
ans = x;
11+
} else if (mx === x) {
12+
ans += x;
13+
}
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)