Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Adobe

Given a sorted array, remove duplicates in-place and return the new length. Adobe tests this because in-place mutation on large pixel/sample arrays is a hot path — every byte allocated is a frame dropped.

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

Source citations

Public interview reports confirming this problem appears in Adobe loops.

  • Glassdoor (2026-Q1)Adobe interviewers ask this to test two-pointer fluency.
  • LeetCode Discuss (2025-08)Common Adobe phone screen.

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 k. The first k elements of nums should hold the final result.

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: Function returns k = 2, with first two elements being 1 and 2.

Example 2

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

Approaches

1. Use a Set, then copy back

Dedup via Set, copy values back into nums.

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

Tradeoff: Violates the in-place requirement — Adobe will dock points for allocating O(n) extra space.

2. Two pointers (write index)

Keep a slow 'write' pointer; advance fast pointer; when nums[fast] != nums[write-1], write nums[fast] to nums[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];
      write++;
    }
  }
  return write;
}

Tradeoff: O(1) extra space, single pass. The two-pointer pattern is the exact same technique used in dedup streaming filters in Adobe's audio/video pipelines.

Adobe-specific tips

Adobe values production-grade in-place mutation here — they're hiring for performance-sensitive code paths (codec dedup, raster scanline compression). Mention that the same write-pointer pattern appears in run-length encoding for TIFF/PSD file compression.

Common mistakes

  • Allocating a new array — defeats the in-place constraint.
  • Comparing with nums[write] instead of nums[read-1] — gets confused on the first iteration.
  • Returning the array — the contract is to return the count and mutate in place.

Follow-up questions

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

  • Remove Duplicates II (LC 80) — allow each value at most twice.
  • Move Zeroes (LC 283) — same two-pointer trick.
  • What if the array isn't sorted? (Set + filter, but breaks in-place property.)

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 compare nums[read] to nums[read-1] and not nums[write-1]?

Because the array is sorted, equal elements are adjacent. Comparing to the previous read position skips runs of duplicates correctly. Comparing to nums[write-1] also works — both are valid.

Does it matter what's after position k?

No — the contract says only the first k elements matter. Garbage after is fine. This matches how Adobe's codec buffers work: a logical length plus an allocated capacity.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →