You're given a sorted array of integers that may contain duplicates. Your task is to remove duplicates in-place so that each unique element appears only once, while maintaining the original sorted order.
The challenge is to do this without using extra space for another array. Instead, you need to modify the original array and return the number of unique elements.
Important: You don't need to worry about elements beyond the first k positions (where k is the number of unique elements). The judge will only check the first k elements of your modified array.
Example: If you have [1,1,2,2,3], you should modify it to [1,2,3,_,_] and return 3. The underscores represent elements that can be ignored.
Input & Output
Visualization
Time & Space Complexity
Single pass through the array with two pointers
Only using two pointer variables, no extra data structures
Constraints
- 1 โค nums.length โค 3 ร 104
- -100 โค nums[i] โค 100
- nums is sorted in non-decreasing order