Skip to content

Commit 6556704

Browse files
authored
Create 2831-find-the-longest-equal-subarray.js
1 parent 11a3f52 commit 6556704

File tree

1 file changed

+19
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)