Given a sorted integer array, remove the repeated values in place so that each distinct number appears only once, keeping their original ascending order. You may not allocate a second array for the answer.
Return the count k of unique elements. The first k slots of the array must hold those unique values in order; whatever remains beyond position k is irrelevant.
Example 1
Input: nums = [1, 1, 2]
Output: 2, nums = [1, 2, _]
Two distinct values remain, so the leading two slots become 1 and 2.
Example 2
Input: nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
Output: 5, nums = [0, 1, 2, 3, 4, _, _, _, _, _]
Five unique numbers are compacted to the front while the tail is left unchecked.
Constraints
1 ≤ nums.length ≤ 3 * 10^4-10^2 ≤ nums[i] ≤ 10^2nums is sorted in non-decreasing orderSee the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI