Remove Duplicates from Sorted Array

Easy

Description

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.

Examples

Input:nums = [1,1,2]
Output:2
Explanation:

Your function should return k = 2, with the first two elements being 1 and 2.

Input:nums = [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.

Input:nums = [-3,-3,-1,-1,0,2,2,2]
Output:4
Explanation:

Your function should return k = 4, with the first four elements being -3, -1, 0, and 2. This example demonstrates handling negative numbers and multiple consecutive duplicates.

Constraints

  • 1 ≤ nums.length ≤ 3 × 10⁴
  • -100 ≤ nums[i] ≤ 100
  • nums is sorted in non-decreasing order.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!