Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ceea3b

Browse files
feat: add solutions to lc problem: No.3005 (doocs#4740)
No.3005.Count Elements With Maximum Frequency
1 parent e89ef4f commit 5ceea3b

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,54 @@ impl Solution {
193193
}
194194
```
195195

196+
#### JavaScript
197+
198+
```js
199+
/**
200+
* @param {number[]} nums
201+
* @return {number}
202+
*/
203+
var maxFrequencyElements = function (nums) {
204+
const cnt = new Array(101).fill(0);
205+
for (const x of nums) {
206+
++cnt[x];
207+
}
208+
let [ans, mx] = [0, -1];
209+
for (const x of cnt) {
210+
if (mx < x) {
211+
mx = x;
212+
ans = x;
213+
} else if (mx === x) {
214+
ans += x;
215+
}
216+
}
217+
return ans;
218+
};
219+
```
220+
221+
#### C#
222+
223+
```cs
224+
public class Solution {
225+
public int MaxFrequencyElements(int[] nums) {
226+
int[] cnt = new int[101];
227+
foreach (int x in nums) {
228+
++cnt[x];
229+
}
230+
int ans = 0, mx = -1;
231+
foreach (int x in cnt) {
232+
if (mx < x) {
233+
mx = x;
234+
ans = x;
235+
} else if (mx == x) {
236+
ans += x;
237+
}
238+
}
239+
return ans;
240+
}
241+
}
242+
```
243+
196244
<!-- tabs:end -->
197245

198246
<!-- solution:end -->

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,54 @@ impl Solution {
191191
}
192192
```
193193

194+
#### JavaScript
195+
196+
```js
197+
/**
198+
* @param {number[]} nums
199+
* @return {number}
200+
*/
201+
var maxFrequencyElements = function (nums) {
202+
const cnt = new Array(101).fill(0);
203+
for (const x of nums) {
204+
++cnt[x];
205+
}
206+
let [ans, mx] = [0, -1];
207+
for (const x of cnt) {
208+
if (mx < x) {
209+
mx = x;
210+
ans = x;
211+
} else if (mx === x) {
212+
ans += x;
213+
}
214+
}
215+
return ans;
216+
};
217+
```
218+
219+
#### C#
220+
221+
```cs
222+
public class Solution {
223+
public int MaxFrequencyElements(int[] nums) {
224+
int[] cnt = new int[101];
225+
foreach (int x in nums) {
226+
++cnt[x];
227+
}
228+
int ans = 0, mx = -1;
229+
foreach (int x in cnt) {
230+
if (mx < x) {
231+
mx = x;
232+
ans = x;
233+
} else if (mx == x) {
234+
ans += x;
235+
}
236+
}
237+
return ans;
238+
}
239+
}
240+
```
241+
194242
<!-- tabs:end -->
195243

196244
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int MaxFrequencyElements(int[] nums) {
3+
int[] cnt = new int[101];
4+
foreach (int x in nums) {
5+
++cnt[x];
6+
}
7+
int ans = 0, mx = -1;
8+
foreach (int x in 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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxFrequencyElements = function (nums) {
6+
const cnt = new Array(101).fill(0);
7+
for (const x of nums) {
8+
++cnt[x];
9+
}
10+
let [ans, mx] = [0, -1];
11+
for (const x of cnt) {
12+
if (mx < x) {
13+
mx = x;
14+
ans = x;
15+
} else if (mx === x) {
16+
ans += x;
17+
}
18+
}
19+
return ans;
20+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /