Skip to main content

5. Remove Element

easyAsked at Coinbase

Remove all occurrences of a target value from an array in place. Coinbase uses this as a quick filter to confirm you reach for two-pointer compaction before allocating.

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

Source citations

Public interview reports confirming this problem appears in Coinbase loops.

  • Glassdoor (2026-Q1)Coinbase phone-screen filter.

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. Modify the array such that the first k elements contain the non-val values.

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

Allocate a new array of non-val elements, copy back.

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

Tradeoff: Easy but allocates. The prompt asks for in-place.

2. Two-pointer in-place

Write pointer tracks the next slot to fill. Iterate the read pointer; if nums[read] != val, copy to write and advance.

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: Linear and in place. When most elements pass, you can do the swap-from-end variant for fewer writes; for most inputs this version is simpler.

Coinbase-specific tips

Coinbase will sometimes ask 'minimize writes' as a follow-up — that's a hint to switch to the swap-from-end variant. Mention both up front to show you understand the tradeoff between write-minimization and code clarity.

Common mistakes

  • Splicing in a loop (nums.splice(i, 1)) — quadratic and mutates length during iteration.
  • Forgetting that the array slots beyond k can be anything — don't waste time clearing them.
  • Returning the array instead of the count.

Follow-up questions

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

  • Minimize the number of writes when val is rare.
  • Generalize to 'remove all elements where predicate(x) is true'.
  • Stream variant: predicate may change over time.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

When does swap-from-end win?

When val occurrences are rare and you want to minimize writes. For each val, swap with the current end and shrink the effective length, so non-val elements stay in place.

Does order matter?

The prompt allows order changes, so swap-from-end is fair game. If order matters, use the simple two-pointer version.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →