Remove Duplicates from Sorted Array
EasyP2Coding
Given an array nums sorted in non-decreasing order, remove the duplicates in place so that each distinct value appears once, keeping the original relative order.
The array must end up holding exactly the distinct values, so shrink it afterwards: in JavaScript set nums.length = k, in Python use del nums[k:], where k is the number of distinct values. Use O(1) extra space; do not build a second array.
Example 1
- Input
- nums = [1,1,2]
- Output
- [1,2]
Example 2
- Input
- nums = [0,0,1,1,1,2,2,3,3,4]
- Output
- [0,1,2,3,4]
Constraints
- 0 <= nums.length <= 3 * 10^4
- -100 <= nums[i] <= 100
- nums is sorted in non-decreasing order
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.