Skip to content

Commit 1a045c0

Browse files
authored
Create 1224-maximum-equal-frequency.js
1 parent 29fe46f commit 1a045c0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

1224-maximum-equal-frequency.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxEqualFreq = function (nums) {
6+
const cnt = {},
7+
freq = {}
8+
let maxF = 0,
9+
res = 0
10+
nums.forEach((num, i) => {
11+
if (cnt[num] == null) cnt[num] = 0
12+
cnt[num] += 1
13+
if (freq[cnt[num] - 1] == null) freq[cnt[num] - 1] = 0
14+
if (freq[cnt[num]] == null) freq[cnt[num]] = 0
15+
freq[cnt[num] - 1] -= 1
16+
freq[cnt[num]] += 1
17+
maxF = Math.max(maxF, cnt[num])
18+
if (
19+
maxF * freq[maxF] === i ||
20+
(maxF - 1) * (freq[maxF - 1] + 1) === i ||
21+
maxF === 1
22+
)
23+
res = i + 1
24+
})
25+
return res
26+
}

0 commit comments

Comments
 (0)