Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Palantir

In-place de-duplicate a sorted array and return the new length. Palantir asks this to test the two-pointer pattern, which is the foundation of any de-dup pass over a sorted entity-resolution candidate stream.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Palantir loops.

  • Glassdoor (2026-Q1)Asked as a precursor to entity-resolution discussion at FDE interviews.
  • LeetCode Discuss (2025-11)Common Palantir warm-up.

Problem

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Return the number of unique elements in nums.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -100 <= nums[i] <= 100
  • nums is sorted in non-decreasing order.

Examples

Example 1

Input
nums = [1,1,2]
Output
2

Explanation: nums becomes [1,2,_]

Example 2

Input
nums = [0,0,1,1,1,2,2,3,3,4]
Output
5

Approaches

1. Use a Set

Insert all elements into a Set, then write back. Violates the in-place requirement.

Time
O(n)
Space
O(n)
function removeDuplicates(nums) {
  const s = new Set(nums);
  const arr = [...s];
  for (let i = 0; i < arr.length; i++) nums[i] = arr[i];
  return arr.length;
}

Tradeoff: O(n) extra space ignores the sorted property. Reject explicitly.

2. Two-pointer in-place compaction

Slow pointer marks next write slot; fast pointer scans. Write whenever the fast pointer finds a value different from the previous write.

Time
O(n)
Space
O(1)
function removeDuplicates(nums) {
  if (nums.length === 0) return 0;
  let write = 1;
  for (let read = 1; read < nums.length; read++) {
    if (nums[read] !== nums[read - 1]) {
      nums[write++] = nums[read];
    }
  }
  return write;
}

Tradeoff: Linear time, constant space. The comparison against nums[read-1] (the source) instead of nums[write-1] (the destination) works because read >= write.

Palantir-specific tips

Palantir likes this problem because it mirrors the de-dup pass in entity resolution: you've already sorted candidate records by composite key and now you collapse exact duplicates before fuzzy-matching the rest. Mention the read >= write invariant and why comparing to nums[read-1] is safe. The bonus signal is articulating that you don't even need to clear the trailing 'garbage' slots — the contract says only the first k positions are meaningful.

Common mistakes

  • Allocating extra storage — fails the in-place requirement.
  • Comparing nums[read] to nums[write-1] instead of nums[read-1] — works but is less clear about the invariant.
  • Off-by-one on the initial write pointer — start at 1, not 0, since nums[0] is always kept.

Follow-up questions

An interviewer at Palantir may pivot to one of these next:

  • Allow duplicates to appear at most twice (LC 80).
  • What if the array is unsorted? (Sort first, then this pass — or use a hash set.)
  • Stream version: emit only the first occurrence of each value from a sorted stream.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does comparing to nums[read-1] still work after we've overwritten earlier slots?

Because read always moves at least as fast as write. By the time we write to position write, the source at position read-1 hasn't been touched yet — it's still the original value.

Do I need to clear out the trailing slots?

No. The problem says only the first k entries need to be correct. Allocating to clear the rest defeats the in-place purpose.

Practice these live with InterviewChamp.AI

Drill Remove Duplicates from Sorted Array and other Palantir interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →