---
# System prepended metadata

title: 2831. Find the Longest Equal Subarray
tags: [Leetcode, Sliding Window, Medium]

---

Link: https://leetcode.com/problems/find-the-longest-equal-subarray/
## 思路
the numbers in sliding window must satisfy the condition that after deleting at most **k** elements, the remain array is a equal array
## Code
```python=
class Solution:
    def longestEqualSubarray(self, nums: List[int], k: int) -> int:
        count = collections.defaultdict(int)
        left, right = 0, 0
        maxFreq = 0
        for right, num in enumerate(nums):
            count[num] += 1
            maxFreq = max(maxFreq, count[num])
            if right-left+1-maxFreq>k:
                count[nums[left]] -= 1
                left += 1
        return maxFreq
```