← All problemsSign in

3940. Limit Occurrences in Sorted Array

LeetCodeArrayTwo PointersOpen on judge ↗

You are given a sorted integer array nums and an integer k. Return an array such that each distinct element appears at most k times, while preserving the relative order of the elements in nums. Note: If a distinct element appears at least k times, then it must appear exactly k times in the resulting array. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,1,2,2,3] Explanation: Each

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start