Remove Duplicates from Sorted Array
Easy
Array
Two Pointers
In-place

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Return the number of unique elements in nums.

Examples:

Input:[1,1,2]
Output:2
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
Input:[0,0,1,1,1,2,2,3,3,4]
Output:5
Explanation: Your function should return k = 5, with the first five elements being 0, 1, 2, 3, and 4.

Constraints:

  • 1 ≤ nums.length ≤ 3 * 10⁴
  • -100 ≤ nums[i] ≤ 100
  • nums is sorted in non-decreasing order.
Code Editor
Loading advanced editor...
Console Output

Ready to execute

Click "Run Code" to see your output here