FastPrepDeduplicate a Sorted Array In Place
Problems/Deduplicate a Sorted Array In Place

Deduplicate a Sorted Array In Place

GEP logoGEP● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for in-place removal of duplicates from a sorted vector. It gives both a two-pointer overwrite and a standard-library approach. It does not give a return value that works across languages, or limits. This version returns the compacted prefix only so that the result of the change can be seen. Our version is about 95% the same as the reported question.

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 interview questions

  • Find the Missing Number from 1 to nOA · Seen Sep 2026
  • Reverse a Singly Linked ListONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Compact Conversation History by Token BudgetSierra · PHONE SCREENSierra interview questions
  • Remove Duplicates from Sorted ArrayNykaa · PHONE SCREENNykaa interview questions
  • Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
  • Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
  • Closest Vehicle and Charger in a LineSpotify · PHONE SCREEN
  • Correct an In-Place Array ReversalDuolingo · PHONE SCREENDuolingo interview questions
  • Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
  • Maximize Distance to the Closest Occupied SeatAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Remove Duplicates From Sorted Array In PlaceGet My Parking · PHONE SCREEN
See GEP hiring insights
Saved
class Solution {
    public int[] deduplicateSortedArray(int[] nums) {
        // Write your code here
    }
}
Checking account…