Skip to content

Commit ebb64cb

Browse files
authored
Update 1224-maximum-equal-frequency.js
1 parent d8966d4 commit ebb64cb

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

1224-maximum-equal-frequency.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxEqualFreq = function (nums) {
6+
const freqCnt = {}, cnt = {}, { max } = Math
7+
8+
let res = 0, maxF = 0, i = 0
9+
for(const e of nums) {
10+
if(cnt[e] == null) cnt[e] = 0
11+
cnt[e]++
12+
13+
const f = cnt[e]
14+
15+
if(freqCnt[f - 1] == null) freqCnt[f - 1] = 0
16+
if(freqCnt[f] == null) freqCnt[f] = 0
17+
18+
if(freqCnt[f - 1] > 0) freqCnt[f - 1]--
19+
freqCnt[f]++
20+
21+
maxF = max(maxF, f)
22+
23+
if(
24+
maxF === 1 ||
25+
maxF * freqCnt[maxF] === i ||
26+
(maxF - 1) * (freqCnt[maxF - 1] + 1) === i
27+
) {
28+
res = i + 1
29+
}
30+
31+
i++
32+
}
33+
34+
return res
35+
}
36+
37+
// another
38+
139
/**
240
* @param {number[]} nums
341
* @return {number}

0 commit comments

Comments
 (0)