Remove Duplicates from Sorted Array
Easy · Two Pointers · Array · Two Pointers
Problem
nums is sorted ascending. Remove the duplicates in place so each value appears once, keeping the order, and return the number of unique values k.
Only the first k positions of nums are checked; whatever sits beyond them does not matter.
Examples
Input: nums = [1,1,2] Output: 2, with nums starting [1,2]
Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, with nums starting [0,1,2,3,4]
Constraints
- 1 <= len(nums) <= 3 * 10^4
- -100 <= nums[i] <= 100
- nums is sorted ascending
Approaches
- Rebuild from a slice assignment — time O(n), space O(n)
Related problems
Start free on Cariara · Coding problems · System design studio