26. Remove Duplicates from Sorted Array
Practice Remove Duplicates from Sorted Array for free on NudgeCode: Easy, Two Pointers pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
nums is sorted in non-decreasing order. Remove the duplicates in place so each distinct value is kept once, in the same order, and return k, the number of distinct values.
After your function returns, the first k elements of nums must be the distinct values in sorted order. Elements after the first k can be anything. Modify nums itself instead of building a new array.
The test checks both k and the first k elements of nums, written as [k, nums[:k]].
Example
nums = [0,0,1,1,1,2,2,3,3,4] -> [5, [0,1,2,3,4]]
The function returns 5, and nums now starts with 0,1,2,3,4.
Constraints
- 1 <= nums.length <= 3 * 10^4
- -100 <= nums[i] <= 100
numsis sorted in non-decreasing order