Skip to main content

5. Remove Element

easyAsked at Plaid

Remove all occurrences of a value from an array in-place and return the new length. Plaid asks this because filtering out pending or void transactions before ETL is exactly the same shape.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid intro pair with LC 26.
  • LeetCode Discuss (2026)Reported as Plaid OA.

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, nums = [2,2,_,_]

Example 2

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

Approaches

1. Filter into a new array, copy back

Build a new array without val, then copy back into nums.

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 allocates. Wastes the in-place opportunity.

2. Two-pointer overwrite

A write pointer trails. When the read sees a non-val, write it.

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

Tradeoff: O(1) extra space. If val is rare you can swap-with-tail instead to save writes, but it's not required here.

Plaid-specific tips

Plaid uses this to verify you handle the in-place constraint cleanly. Bonus signal: ask whether stability matters. In their pipeline, transaction order is sacred for daily reconciliation, so the order-preserving variant is the right default — only relax it if they explicitly say order doesn't matter.

Common mistakes

  • Returning the array slice — the prompt asks for the length.
  • Forgetting that the leftover tail is allowed to contain anything — don't waste time zeroing it.
  • Writing nums[read] = ... instead of nums[write++] = nums[read].

Follow-up questions

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

  • If val is rare, swap-from-tail to minimize writes — useful when writes are expensive (e.g., DB rows).
  • Remove all elements satisfying a predicate, not just one value.
  • Stream version: process a file too large to fit in memory.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Is the leftover memory beyond k tested?

No — LC explicitly tests only nums[0..k-1]. In a real Plaid pipeline you'd null-out the tail to help GC, but here it's wasted work.

When would you prefer swap-with-tail?

When writes are expensive — e.g., the array represents disk rows. Filter-and-shift is O(n) writes; swap-with-tail is at most O(k) writes where k is the count of val.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →