Remove Duplicates From Sorted Array In Place
Problem statement
You are given an integer array nums sorted in nondecreasing order. Modify it in place so that each distinct value appears exactly once and the retained values stay in sorted order.
Return an array containing exactly the compacted prefix. Extra capacity after that prefix may be left unchanged.
Function
removeDuplicates(nums: int[]) → int[]Examples
Example 1
nums = [1,1,2]return = [1,2]The sorted values are 1, 1, and 2. After in-place compaction the unique prefix is [1,2].
Example 2
nums = [0,0,1,1,1,2,2,3,3,4]return = [0,1,2,3,4]Adjacent duplicates are overwritten so each retained value appears once. The compacted prefix is [0,1,2,3,4].
Example 3
nums = []return = []An empty array has no values to retain, so the compacted prefix is empty.
Constraints
0 <= nums.length <= 10^5.-10^9 <= nums[i] <= 10^9.numsis sorted in nondecreasing order.