Remove Duplicates from Sorted Array

Given a sorted integer array nums, remove the duplicates in-place such that each unique element appears only once. Return the new length of the array after removing duplicates. The first k elements of nums should hold the unique values.

Input Format

nums = sorted array of integers

Output Format

Return k where k is number of unique elements; first k elements are unique.

Constraints

  • 1 <= nums.length <= 3 * 10^4; -100 <= nums[i] <= 100

Examples

Example 1:

Input:

nums = [1,1,2]

Output:

2

Explanation:

After removing duplicates, the first two elements are [1,2].

Loading...
Remove Duplicates from Sorted Array - Arrays