Skip to main content

5. Remove Element

easyAsked at Adobe

Given an array and a value, remove every occurrence in-place and return the new length. Adobe asks this to test in-place mutation discipline that maps directly to pixel-mask filtering in image-editor pipelines.

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

Source citations

Public interview reports confirming this problem appears in Adobe loops.

  • Glassdoor (2026-Q1)Adobe SDE-I screens use this as a two-pointer warm-up.
  • LeetCode Discuss (2025-07)Frequently paired with LC 26 in Adobe rounds.

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.

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 place

Repeatedly splice out the value.

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

Tradeoff: Each splice is O(n), so worst case is O(n^2). Mention only as the anti-pattern.

2. Two pointers (write index)

Walk read across the array; copy nums[read] to nums[write] only when it's not 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: O(n) single pass, O(1) space. The same write-pointer trick reappears in alpha-channel filtering: walk the pixel stream and write only the non-transparent pixels into the output buffer.

Adobe-specific tips

Adobe interviewers will probe whether you understand that the elements after the returned length are 'don't care' — same contract as a frame buffer with a logical width less than its allocated stride. Mention that you can also use a swap-with-last approach when order doesn't matter, useful for unordered sprite lists.

Common mistakes

  • Trying to delete via splice in a forward loop — index shifts cause skipped elements.
  • Returning the array instead of the count — re-read the contract.
  • Assuming the array is sorted — it's not.

Follow-up questions

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

  • What if you need to preserve original order? (This solution does.)
  • What if you want to count, not remove? (Single counter, no writes.)
  • Move Zeroes (LC 283) — same pattern, fixed val=0.

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 swap-with-last approach also valid?

Yes, when order doesn't matter. It can be faster when removals are rare because you don't write each non-val element. Adobe often accepts either as long as you discuss the trade-off.

Why don't we clear the trailing positions?

The problem says the elements beyond k are 'don't care'. Leaving stale data is a feature, not a bug — same as how image-editing software treats out-of-bounds canvas regions.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →