Remove Duplicates from Sorted Array
Given a sorted integer array nums, remove duplicates in place so each unique value appears once, keeping their relative order. Return k, the number of unique elements, with the first k slots of nums holding those unique values (the rest may be anything).
Open official problem prompt ↗Compact a sorted array in place so each value survives exactly once, and report how many survive.
Shelving sorted books: you keep a bookmark at the last unique title placed; as you flip through, any time a new title appears you slide it up next to the bookmark and move the bookmark forward.
- Input
- nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
- Output
- 5, nums = [0, 1, 2, 3, 4, _, _, _, _, _]
- Why
- There are 5 distinct values 0,1,2,3,4; they are written into the front of the array and k = 5 is returned.
1 <= nums.length <= 3 * 10^4-100 <= nums[i] <= 100nums is sorted in non-decreasing order