File tree Expand file tree Collapse file tree 4 files changed +134
-0
lines changed
solution/3000-3099/3005.Count Elements With Maximum Frequency Expand file tree Collapse file tree 4 files changed +134
-0
lines changed Original file line number Diff line number Diff line change @@ -193,6 +193,54 @@ impl Solution {
193
193
}
194
194
```
195
195
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
+
196
244
<!-- tabs: end -->
197
245
198
246
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -191,6 +191,54 @@ impl Solution {
191
191
}
192
192
```
193
193
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
+
194
242
<!-- tabs: end -->
195
243
196
244
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments