Skip to main content

5. Remove Element

easyAsked at Datadog

Given an array and a value, remove all occurrences in place and return the new length. Datadog uses this as a hello-world for in-place stream filtering — same pattern as their per-tag drop-rule applied to incoming logs.

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 part of Datadog OA pool.
  • LeetCode Discuss (2025-10)Cited in Datadog tagged problem set.

Problem

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Constraints

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Examples

Example 1

Input
nums = [3,2,2,3], val = 3
Output
2, nums = [2,2,_,_]

Example 2

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

Approaches

1. Filter + rebuild

Filter out val, then copy back.

Time
O(n)
Space
O(n)
function removeElement(nums, val) {
  const kept = nums.filter(x => x !== val);
  for (let i = 0; i < kept.length; i++) nums[i] = kept[i];
  return kept.length;
}

Tradeoff: Allocates a new array. Datadog will fail you for not doing it in-place when explicitly asked.

2. Two-pointer in-place (optimal)

k tracks the next write slot. Scan with i; whenever nums[i] != val, copy to nums[k] and increment k.

Time
O(n)
Space
O(1)
function removeElement(nums, val) {
  let k = 0;
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] !== val) {
      nums[k] = nums[i];
      k++;
    }
  }
  return k;
}

Tradeoff: Single pass, O(1) extra space. Direct mapping to streaming filter — same shape as a Datadog pipeline transform.

Datadog-specific tips

Datadog will probe ordering: 'Do you preserve relative order? What if you didn't have to?' Show you know the swap-with-end variant (faster when val is rare). Mention how this generalizes to a streaming filter operator.

Common mistakes

  • Skipping increment of i after a swap-with-end — could miss checking the newly-swapped element.
  • Returning nums or nums.length instead of k.
  • Allocating a new array when in-place was required.

Follow-up questions

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

  • Remove Duplicates from Sorted Array (LC 26).
  • Move Zeroes — same pattern but val is implicit (LC 283).
  • Partition array by predicate — generalization.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Does order matter?

The problem says order can change. If order DID matter, the two-pointer write approach above preserves relative order anyway.

When would you swap-with-end instead?

When val is rare and most elements are kept. Swap-with-end performs fewer writes than copy-forward.

Practice these live with InterviewChamp.AI

Drill Remove Element 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 →