LeetCode 2958, Medium. Topics: Array, Hash Table, Sliding Window. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 25 parametrized pytest cases, and a playground notebook:
Problem
You are given an integer array nums and an integer k.
The frequency of an element x is the number of times it occurs in an array.
An array is called good if the frequency of each element in this array is less than or equal to k.
Return the length of the longest good subarray of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Examples
Constraints
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026