Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Figma

Given a sorted array, remove duplicates in place and return the new length. Figma frames this as 'compact the operation log after dedup,' a near-daily concern when CRDT clients send overlapping ops during reconnect.

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

Source citations

Public interview reports confirming this problem appears in Figma loops.

  • LeetCode Discuss (2025)Figma OA easy section.
  • Glassdoor (2026-Q1)Phone-screen warm-up for IC3.

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
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 + rewrite

Push everything into a Set, then copy back into nums.

Time
O(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: O(n) extra space. Ignores that the input is sorted, so duplicates are adjacent.

2. Two pointers (write index)

Walk with a read pointer; advance a write pointer only when the value changes. Sorted-adjacent property does the dedup for free.

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: In-place, O(1) extra. The invariant 'nums[0..write-1] holds the deduped prefix' is the key insight.

Figma-specific tips

Figma loves the two-pointer pattern because their CRDT op-log compaction does the same thing — read forward, write only when something new appears. Articulate the write-index invariant explicitly: 'nums[0..write-1] is the deduped prefix so far.' That phrasing is recognizable shorthand for systems-thinking at Figma.

Common mistakes

  • Starting write at 0 — overwrites the first element. Start at 1 since nums[0] is already deduped.
  • Comparing nums[read] to nums[write-1] when nums[read-1] is simpler and equivalent given the sort.
  • Forgetting the empty-array guard.

Follow-up questions

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

  • Remove duplicates allowing at most 2 repeats (LC 80).
  • Move Zeroes (LC 283) — same two-pointer skeleton.
  • Remove Duplicates from Sorted List (LC 83) — linked-list variant.

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 sorted input matter?

Because duplicates are guaranteed to be adjacent, you only need to compare with the previous element. On an unsorted array you'd need a hash set.

What about the elements past index k?

The problem says you can leave them as anything — they're not part of the answer. Don't waste cycles zeroing them.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →