24. Remove Duplicates from Sorted Array

Given an integer array nums sorted in non-decreasing order. Remove duplicates in place so that each unique element appears only once. Relative order must be preserved.
Let the number of unique elements be k. After removing duplicates, return k.
  • The first k elements of nums must contain the unique values in sorted order.
  • The remaining elements (after index k - 1) can be ignored.
Example 1
Input: nums = []
Output: 0
Explanation: Empty array — length 0.
Example 2
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5
Explanation: After removing consecutive duplicates we get [0,1,2,3,4], length 5.
Example 3
Input: nums = [1,1,2]
Output: 2
Explanation: Remove consecutive duplicates: [1,1,2] -> [1,2], new length 2.
arraysmedium
JavaScript
Loading...
Line 1, Char 1