Skip to main content

26. Remove Duplicates from Sorted Array

easyAsked at Intel

Given a sorted array, remove duplicates in-place such that each unique element appears once. Return the new length. Intel asks because the two-pointer pattern is identical to Move Zeroes but here the invariant is 'last unique value' rather than 'last non-zero' — same trick, different predicate.

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

Source citations

Public interview reports confirming this problem appears in Intel loops.

  • Glassdoor (2026-Q1)Intel SWE phone-screen reports cite remove-duplicates as a recurring two-pointer warm-up.
  • GeeksforGeeks (2025-09)Intel Interview Experience archives reference two-pointer in-place dedup.
  • LeetCode discuss (2025-11)Intel-tagged in the LC company-frequency listing.

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. Then return the number of unique elements in nums. Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things: change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums. Return k.

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

Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.

Example 2

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

Approaches

1. Set + rewrite (brute)

Collect unique values into a Set, write them back, return its size.

Time
O(n)
Space
O(n)
function removeDuplicatesSet(nums) {
  const set = new Set(nums);
  const unique = [...set]; // Set preserves insertion order; on sorted input this is the dedup
  for (let i = 0; i < unique.length; i++) nums[i] = unique[i];
  return unique.length;
}

Tradeoff: Throws away the 'sorted' precondition. Same Big-O time but O(n) space. Mention only as the wrong-shape approach.

2. Two-pointer in-place (optimal)

Maintain a write pointer at the end of the unique prefix. Walk read forward; when nums[read] differs from nums[write], advance write and copy.

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

Tradeoff: Linear time, constant space, in-place. Because the array is sorted, duplicates are adjacent — so comparing to nums[write] (the last unique value) is enough to detect a new unique value. The 'sorted' precondition is what unlocks O(1) space; if unsorted, you'd need a hash set.

Intel-specific tips

Intel reviewers want you to articulate the role of 'sorted': 'Because the array is sorted, all copies of any value are adjacent. So a new unique element is signaled by nums[read] != nums[write]. Without the sorted invariant, I'd need a hash set and O(n) space.' That sentence is the senior signal. The variant LC 80 (allow duplicates up to twice) is also fair game as a follow-up — same skeleton with a count check.

Common mistakes

  • Comparing nums[read] to nums[read - 1] instead of nums[write] — works on the canonical input but breaks if a duplicate spans the read-write gap.
  • Returning the array — the problem wants the length, with the array mutated in-place.
  • Off-by-one on the return: it's write + 1 (count), not write (index).
  • Forgetting the empty-array base case — `nums.length === 0` should return 0.

Follow-up questions

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

  • Remove Duplicates from Sorted Array II (LC 80) — allow each unique value up to twice.
  • Remove Element (LC 27) — same two-pointer with a target value filter.
  • What if the input is unsorted? (Sort first — O(n log n) — or use a hash set with O(n) space.)
  • What if the array is a linked list? (LC 83 Remove Duplicates from Sorted List — same idea, pointer manipulation.)

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 two pointers and not one?

The read pointer scans forward; the write pointer tracks where the next unique element should go. They diverge whenever you skip a duplicate. With one pointer you can detect duplicates but you can't place the next unique element without remembering the destination index.

Why is the 'sorted' precondition critical?

Sorting groups identical values together. So the latest unique value is always at nums[write], and the next-read either equals it (skip) or differs (advance). Without sortedness, the same value could appear non-adjacently and the simple comparison would miss the duplicate.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →