Skip to main content

4. Remove Duplicates from Sorted Array

easyAsked at Datadog

Given a sorted array, remove duplicates in place and return the new length. Datadog uses this to test two-pointer mechanics — the same pattern they use for compacting sorted metric chunks before compression.

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

Source citations

Public interview reports confirming this problem appears in Datadog loops.

  • Glassdoor (2026-Q1)Reported as a 15-minute warmup at Datadog NYC.
  • LeetCode Discuss (2025-11)Listed in Datadog tagged problem set.

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 after deduplication.

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

Throw values into a Set, sort, copy back.

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

Tradeoff: Throws away the sorted-input invariant; allocates O(n) memory. Anti-pattern for streaming compaction.

2. Two-pointer in-place (optimal)

Slow pointer marks write position; fast scans. When nums[fast] != nums[slow], advance slow and copy.

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

Tradeoff: Single pass, O(1) extra space. Mirrors how Datadog's ingestion compacts repeated metric points before persistence.

Datadog-specific tips

Datadog will follow up with: 'Now stream the input — don't load the whole array.' Show that the slow/fast pattern generalizes to a one-pass dedup over an infinite sorted stream by emitting whenever the new value differs from the last-emitted one.

Common mistakes

  • Returning the array instead of the length — the problem asks for k.
  • Off-by-one in slow+1 vs slow — the new length is slow + 1.
  • Forgetting the empty-array guard — accessing nums[0] crashes on [].

Follow-up questions

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

  • Remove Duplicates II — allow at most 2 occurrences of each value (LC 80).
  • Remove Element — drop all instances of a given value (LC 27).
  • Dedup a sorted linked list (LC 83).

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

It guarantees that duplicates are adjacent. The two-pointer dedup only works because nums[fast] == nums[slow] iff fast is in a run of duplicates.

Can this work on unsorted input?

Not in O(n) with O(1) space — you'd need a hashset, blowing space to O(n).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →