Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Coinbase

Compact a sorted array in place so each element appears once and return the new length. Coinbase uses this to probe pointer-discipline — the same two-pointer trick deduplicates trade prints with the same sequence number.

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

Source citations

Public interview reports confirming this problem appears in Coinbase loops.

  • Glassdoor (2026-Q1)Coinbase backend phone screen, warm-up before idempotency follow-up.
  • LeetCode Discuss (2025)Reported as Coinbase opener with a dedup-trade-stream follow-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 k after placing the final result in the first k slots of 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, nums = [1,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. Set + rebuild

Build a Set, sort it, and copy back.

Time
O(n log n)
Space
O(n)
function removeDuplicates(nums) {
  const u = [...new Set(nums)].sort((a, b) => a - b);
  for (let i = 0; i < u.length; i++) nums[i] = u[i];
  return u.length;
}

Tradeoff: Throws away the sorted-input invariant. Allocates extra memory the prompt explicitly forbids.

2. Two-pointer in-place

Use a write pointer that lags the read pointer. Advance the read pointer through the array; when nums[read] != nums[write-1], write it and advance the write pointer.

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: Linear pass, no extra memory. The two-pointer technique generalizes to compacting any 'keep elements that satisfy P' filter.

Coinbase-specific tips

Coinbase grades on the two-pointer pattern AND on whether you check the boundary correctly. They'll often follow with 'now deduplicate a stream of trades where duplicates have the same sequence number' — show you understand idempotency by sequence id is the same shape as this.

Common mistakes

  • Starting write at 0 — overwrites the first element you need to keep.
  • Comparing nums[read] to nums[write] instead of nums[read-1] — wrong with all-duplicate inputs.
  • Resizing the array instead of returning k — the prompt asks for length, not a new array.

Follow-up questions

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

  • Allow at most 2 duplicates of each value (LC 80).
  • Dedup a stream of (seq_id, payload) pairs where duplicates by seq_id must be ignored.
  • The array is no longer sorted — keep first occurrence only.

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

Either works for sorted input because nums[write-1] equals the most recently kept value, which equals nums[read-1] when consecutive duplicates exist. nums[read-1] is slightly clearer in intent.

What changes if the input is unsorted?

You need a Set to remember what you've seen — O(n) extra space. The two-pointer trick only works because sorted inputs guarantee duplicates are adjacent.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →