We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 29fe46f commit 1a045c0Copy full SHA for 1a045c0
1224-maximum-equal-frequency.js
@@ -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