Squash Repeats in Place
nums is sorted in non-decreasing order. Rearrange it in place so that each distinct value appears exactly once, in increasing order, at the front of the list, and return n, the number of distinct values.
Only the first n items of nums are checked; whatever sits after them does not matter. A single pass with a read index and a write index is enough, because the write index never gets ahead of the read index.
Example 1
nums [1,1,2]
returns [1,2]
Return 2; the first two items must be [1, 2].
Example 2
nums [0,0,1,1,1,2,2,3,3,4]
returns [0,1,2,3,4]
Return 5; the first five items must be [0, 1, 2, 3, 4].
Constraints
0 <= len(nums) <= 100-1000 <= nums[i] <= 1000numsis sorted in non-decreasing order
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.