112. Remove Duplicates from Sorted Array

Easy · Array

Given a sorted array of integers, remove all duplicate elements in-place such that each unique element appears only once. Return the number of unique elements.

Modify the array so that the first k elements contain the unique values in their original order, where k is the count of unique elements. The elements after index k-1 can be anything (they won't be checked).

Examples

Example 1
Input: [1, 1, 2]
Output: 2, array becomes [1, 2, _]
Explanation: The array has 2 unique elements (1 and 2). We return 2, and the first 2 elements of the array are [1, 2].
Example 2
Input: [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
Output: 5, array becomes [0, 1, 2, 3, 4, _, _, _, _, _]
Explanation: The array has 5 unique elements. We return 5, and the first 5 elements are [0, 1, 2, 3, 4].

Constraints