Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Snap

Modify a sorted array in-place so each unique element appears once, returning the new length. Snap uses this to test in-place mutation discipline and two-pointer instincts.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snap loops.

  • LeetCode Discuss (2025)Reported as a common Snap warm-up that tests in-place editing.
  • Glassdoor (2026-Q1)Snap recruiters cite in-place array problems for new-grad screens.

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 k, the number of unique elements, with the first k positions of nums containing them.

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
k = 2, nums = [1,2,_]

Example 2

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

Approaches

1. Set then overwrite

Insert into a Set, sort the set's keys, overwrite the array.

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

Tradeoff: Ignores the sorted invariant and uses O(n) extra memory. Doesn't earn the 'in-place' signal.

2. Two-pointer in-place (optimal)

Maintain a slow pointer for the next unique slot. Advance a fast pointer through nums; when nums[fast] differs from nums[slow-1], copy it to nums[slow] and increment slow.

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

Tradeoff: Truly in-place with O(1) extra space. Reads the sorted invariant directly by comparing consecutive values.

Snap-specific tips

Snap is explicit about the 'in-place' constraint in screens — call out that you understand 'allocate no extra array' and that you'll respect O(1) auxiliary space. Bonus signal at Snap: link the two-pointer pattern to how their dedup of identical Stories in a feed works without rebuilding the array.

Common mistakes

  • Comparing nums[fast] with nums[fast - 1] vs nums[slow - 1] — both work, but be consistent.
  • Allocating a fresh array even though the prompt says in-place.
  • Returning the array instead of the length — re-read the signature.

Follow-up questions

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

  • Allow each unique element to appear at most twice (LC 80).
  • Generalize to allow each element to appear at most k times.
  • Work on a singly-linked list with the same invariant.

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 the sorted invariant matter?

Because duplicates are guaranteed to be adjacent, a single sweep with a 'previous' comparison is enough. Without sorting, you'd need a hash set, which costs O(n) extra memory.

Are positions beyond k guaranteed to be cleared?

No — they may contain stale values. The contract says only the first k positions are meaningful. Don't waste time zeroing the tail unless the prompt asks for it.

Companies that also ask Remove Duplicates from Sorted Array