Remove Duplicates from Sorted Array

Given a sorted array, remove duplicates in-place so that each unique element appears once and return the new length. Since equal values are adjacent, a two-pointer overwrite strategy is enough.

Input Format

nums = sorted array of integers

Output Format

k, the number of unique elements

Constraints

  • 1 <= nums.length <= 3 * 10^4; -100 <= nums[i] <= 100; nums is sorted in non-decreasing order

Examples

Example 1:

Input:

nums = [1,1,2]

Output:

2

Explanation:

The unique prefix becomes [1,2].

Example 2:

Input:

nums = [0,0,1,1,1,2,2,3,3,4]

Output:

5

Explanation:

The unique elements are [0,1,2,3,4].

Loading...
Remove Duplicates from Sorted Array