Remove Duplicates from Sorted Array
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Write removeDuplicates(nums): nums is sorted in ascending order and may repeat values. Change nums in place so that its first k items are each distinct value once, still in order, and return k. What's left after the first k items doesn't matter, and an empty array gives 0. The checks read nums itself, so don't just build a new array. For example, with nums = [1, 1, 2, 3, 3] it returns 3, and nums starts [1, 2, 3]. The checks include 200,000 numbers, so shifting everything along for each repeat is too slow.
Your code must pass
- Returns how many distinct values there are
- Moves the distinct values to the front, in order
- Handles an empty array and one without repeats
- Is fast enough for 200,000 numbers