Skip to main content

5. Remove Element

easyAsked at ServiceNow

Remove every occurrence of a target value from an array in place, returning the new length. ServiceNow uses this to confirm you understand in-place compaction — the same trick they use when purging soft-deleted incident rows from a result buffer.

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

Source citations

Public interview reports confirming this problem appears in ServiceNow loops.

  • Glassdoor (2026-Q2)Frequently paired with Remove Duplicates as a 'pick one' phone screen.
  • LeetCode Discuss (2025-10)ServiceNow loops reportedly mention the analogy to purging tickets by status.

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 that 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,4,0,3,_,_,_]

Approaches

1. Splice in a loop

Iterate; when nums[i] === val, splice it out and continue.

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

Tradeoff: Each splice is O(n) due to shifting. Mention as the naive choice but show why ServiceNow prefers the pointer pattern.

2. Two-pointer write-pointer

Walk a read pointer over nums. Maintain a write pointer; copy nums[read] to nums[write] only when nums[read] != val.

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 time, constant space. Order-preserving — and ServiceNow likes that, since their ticket logs are timestamp-ordered.

ServiceNow-specific tips

ServiceNow grades for in-place mutation discipline — they want one read pointer, one write pointer, and a clear invariant 'nums[0..write) is the kept prefix.' Bonus signal: note that if duplicates of val are rare, you can use the swap-with-end variant for fewer writes — same insight as in CMDB bulk-delete passes.

Common mistakes

  • Confusing read and write pointers when both happen to be at the same index — the assignment is still correct, just wasted.
  • Returning nums.length instead of the write pointer — fails because the array hasn't been shrunk.
  • Using indexOf in a loop — that's O(n^2) and shows up as a perf flag in their auto-grader.

Follow-up questions

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

  • What if you should minimize writes when val is rare? (Swap with last and shrink.)
  • Remove duplicates from sorted array (LC 26) — same pattern.
  • Move zeroes (LC 283) — same pattern but with a final cleanup pass.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Order doesn't matter — can I be smarter?

Yes — when nums[i] === val, swap it with nums[last] and decrement last. Each removal is one swap. That's better when val is rare.

Should I clear the tail of the array?

No — the contract says only the first k elements matter. Clearing wastes work the grader won't read.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →