FastPrepDeduplicate a Sorted Array In Place
Problem · Array

Deduplicate a Sorted Array In Place

Learn this problem
EasyGEP logoGEPINTERNPHONE SCREEN

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. 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.
  • nums is sorted in nondecreasing order.

More GEP problems

drafts saved locally
public int[] deduplicateSortedArray(int[] nums) {
    // Write your code here
}
nums[1,1,1,2,2,3,3]
expected[1,2,3]
checking account