Skip to content

Commit 0105aaa

Browse files
authored
Create 2958-length-of-longest-subarray-with-at-most-k-frequency.js
1 parent 33ef71d commit 0105aaa

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const maxSubarrayLength = function(nums, k) {
7+
const n = nums.length, cnt = {}, {max} = Math
8+
let i = 0, res = 0
9+
for(let j = 0; j < n; j++) {
10+
const e = nums[j]
11+
if(cnt[e] == null) cnt[e] = 0
12+
cnt[e]++
13+
while(cnt[e] > k) {
14+
const tmp = nums[i]
15+
cnt[tmp]--
16+
i++
17+
}
18+
res = Math.max(res, j - i + 1)
19+
}
20+
21+
return res
22+
};

0 commit comments

Comments
 (0)