Skip to main content

5. Remove Element

easyAsked at Intuit

Given an array and a value, remove all instances of that value in-place and return the new length. Intuit asks this as a basic two-pointer exercise — analogous to filtering out 'voided' transactions from a ledger before reconciliation.

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

Source citations

Public interview reports confirming this problem appears in Intuit loops.

  • Glassdoor (2025-10)Intuit phone-screen, framed as 'remove voided transactions from this list'.
  • LeetCode Discuss (2026-Q1)QuickBooks SWE screen mention.

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. The first k slots of nums must contain the elements not equal to val; the rest may be anything.

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 into new array

Allocate a new array containing only non-val elements; copy back into nums.

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

Tradeoff: Linear time but O(n) extra memory — violates the in-place requirement.

2. Two-pointer in-place (optimal)

Maintain a write pointer. For each read pointer, if the value isn't val, copy to write and advance write.

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];
      write++;
    }
  }
  return write;
}

Tradeoff: Single pass, O(1) extra space. Order is preserved among kept elements.

Intuit-specific tips

Intuit interviewers may follow up with 'what if val is the most common value? Could you swap-from-end to skip writes?' — that's the LC follow-up. Bonus signal: discuss the trade-off (swap-from-end is faster when many match, but breaks order; two-pointer preserves order). For reconciliation use cases, preserving order matters.

Common mistakes

  • Decrementing read inside the loop when swapping from the end — causes infinite loops if not careful.
  • Returning the array instead of the count.
  • Off-by-one when val is the last element of the array.

Follow-up questions

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

  • Remove all occurrences of multiple values in one pass.
  • Remove voided transactions where 'voided' is a boolean field on each row.
  • Stable partition: keep order of both groups (kept + removed) for audit logs.

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 order not matter per the prompt?

It's a relaxation to let you use the swap-from-end optimization. Most real Intuit use cases require stable order, so default to two-pointer.

Can I just splice() to remove?

splice is O(n) per call, so n removals = O(n^2). The two-pointer is strictly better.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →