Problem · Array
Deduplicate a Sorted Array In Place
Learn this problemProblem 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. Use O(1) auxiliary workspace for the compaction; the returned array used to expose that prefix to the judge is not counted as auxiliary workspace.
Function
deduplicateSortedArray(nums: int[]) → int[]Examples
Example 1
nums = [1,1,1,2,2,3,3]return = [1,2,3]The first copy of each distinct value is retained and all later copies are removed.
Example 2
nums = [-2,-2,0,0,0,5]return = [-2,0,5]The compacted sorted prefix contains the three distinct values -2, 0, and 5.
Example 3
nums = []return = []An empty array already contains no duplicates.
Constraints
0 <= nums.length <= 100000.-1000000000 <= nums[i] <= 1000000000.numsis sorted in nondecreasing order.