FastPrepMake a Sorted Array Unique In Place
Problems/Make a Sorted Array Unique In Place

Make a Sorted Array Unique In Place

Alpaca logoAlpaca● EasyFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks candidates to make an array unique only by mutating the input. Because it omits return mechanics, this practice adapter uses the conventional sorted-array write-prefix contract and returns that unique prefix; the judged core matches about 85%.

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.

What the interview report shared

The interviewer asked an easy coding question: remove duplicate elements in a sorted array in place. The candidate solved it in a shared Google Doc.

Function

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

More Alpaca problems

  • Return Distinct ElementsPHONE SCREEN · Seen Aug 2026
  • Hungarian Lottery Winner CountsOA · Seen Jun 2026
  • Aggregate a Large Dataset With GROUP BYPHONE SCREEN · Seen Sep 2022
See Alpaca hiring insights
Saved
class Solution {
    public int[] makeUniqueInPlace(int[] nums) {
      // Mutate nums and return its unique prefix.
    }

}
Checking account…