Skip to main content

5. Remove Element

easyAsked at Workday

Remove all occurrences of a value in-place from an array. Workday tests this for terminated-employee scrubbing — strip out IDs marked for deactivation from an active-roster array.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025-Q4)Workday SDE1 phone screen warmup.

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 (k).

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. Splice in a loop

Loop and splice each occurrence out.

Time
O(n^2)
Space
O(1)
for (let i = 0; i < nums.length;) {
  if (nums[i] === val) nums.splice(i, 1);
  else i++;
}
return nums.length;

Tradeoff: Each splice shifts the suffix — O(n) per call, O(n^2) total.

2. Two-pointer write

Slow pointer = next write slot; fast scans. Write only when not val.

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

Tradeoff: Single pass, O(1) extra space. Order is preserved as a bonus.

Workday-specific tips

Workday grades for recognizing this as the 'partition' pattern. Bonus signal: mention the swap-with-end variant when the order doesn't matter (saves writes when val is rare). Cite payroll deactivation as the real use case.

Common mistakes

  • Using splice — quadratic for no reason.
  • Decrementing fast when you write — breaks the invariant.
  • Returning nums.length instead of k.

Follow-up questions

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

  • Remove Duplicates from Sorted Array (LC 26).
  • What if val is rare? Optimize for fewer writes (swap-with-end).
  • Move Zeroes (LC 283) — same pattern, value = 0, keep order.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Can I use Array.filter?

It works but allocates a new array — not in-place. The prompt asks for in-place modification.

Why is order preserved with this approach?

Non-val elements are written in the order encountered. The swap-with-end variant doesn't preserve order but minimizes writes.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →