LeetCode daily problem 2958. Length of Longest Subarray With at Most K Frequency, was a fun one, I used a sliding window and a dictionary that kept count of each value's frequency, while the right pointer's frequency was above K I adjusted the frequency count, and left pointer. I believe the time & space complexity are O(n) linear. Did you give it a try? Link to my solution.
https://github.com/cardosopab/LeetCodeNotes/blob/main/2024/03/2958.py