Skip to main content

5. Remove Element

easyAsked at Reddit

Remove all occurrences of a target value in-place from an array. Reddit asks this to test in-place mutation patterns used in their server-side comment-tree pruning when a moderator nukes a banned user's history.

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

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen, common pairing with comment-moderation follow-ups.

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

Explanation: nums becomes [2,2,_,_] and we return 2.

Example 2

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

Approaches

1. Filter into new array

Build a new array of values != val, 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: Linear time but O(n) extra space — wasted because the in-place version is one extra pointer.

2. Two-pointer in-place (optimal)

Write pointer 'k' marks next slot. Walk read pointer; copy when nums[read] != val.

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: O(1) extra memory. If val is rare, an alternative swap-from-end version makes fewer writes.

Reddit-specific tips

Reddit interviewers want you to ask: 'does order matter?' If they say no, the swap-from-end variant makes fewer writes, which matters at scale. Bonus signal: connect to their content-moderation pipeline where they bulk-prune nodes from a comment tree.

Common mistakes

  • Modifying nums while iterating with a for-of loop and getting confused indices.
  • Returning the array (the grader wants the length).
  • Forgetting that the trailing slots may hold any values — they don't have to be cleared.

Follow-up questions

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

  • Remove duplicates from sorted array (LC 26).
  • Sort colors (LC 75) — 3-way partition variant.
  • Move zeroes to end (LC 283).

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 two pointers instead of splice?

Array splice is O(n) per call, leading to O(n^2) total.

Swap-from-end vs. compact — which is better?

Compact does up to n-1 writes always. Swap-from-end does at most one write per deletion. If deletions are rare, swap-from-end wins.

Practice these live with InterviewChamp.AI

Drill Remove Element and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →