Remove Duplicates from Sorted Array II

Medium

Description

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Return the number of elements after removing extra duplicates.

Examples

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

The function returns 5 with the first five elements being [1,1,2,2,3].

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

The function returns 7 with the first seven elements being [0,0,1,1,2,3,3].

Constraints

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

Ready to solve this problem?

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