5. Remove Element
easyAsked at VercelRemove all occurrences of a value from an array in-place and return the new length. Vercel asks this because it's the in-place filter that shows up everywhere in their middleware request-rewriting code path.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2026-Q1)— Vercel edge middleware screen warm-up.
- LeetCode Discuss (2025-11)— Listed in Vercel runtime engineer interview pool.
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 should contain the kept elements.
Constraints
0 <= nums.length <= 1000 <= nums[i] <= 500 <= val <= 100
Examples
Example 1
nums = [3,2,2,3], val = 32, nums = [2,2,_,_]Example 2
nums = [0,1,2,2,3,0,4,2], val = 25, nums = [0,1,4,0,3,_,_,_]Approaches
1. Filter into a new array
Use Array.filter and copy back.
- Time
- O(n)
- Space
- O(n)
function removeElement(nums, val) {
const kept = nums.filter(x => x !== val);
for (let i = 0; i < kept.length; i++) nums[i] = kept[i];
return kept.length;
}Tradeoff: Allocates a second array, violating in-place. Mention only as the obvious starting point.
2. Two-pointer in-place (optimal)
Slow pointer is the write head. Walk the array with fast; when nums[fast] !== val, copy to nums[slow] and advance slow.
- 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: O(1) extra space, single pass. If val appears rarely, you'd consider swapping with the tail to skip copies — but the simple version is preferred unless the interviewer pushes.
Vercel-specific tips
Vercel values the in-place version; they explicitly want you to mutate the array argument. Bonus signal: pointing out that 'order may be changed' enables a swap-with-tail variant if val is rare. They like when you weigh the two variants out loud before picking.
Common mistakes
- Mutating while iterating with splice() — O(n^2) and bug-prone because indices shift.
- Returning the original nums.length — the grader expects k, not n.
- Confusing this with LC 26 (remove duplicates) and writing the unique-array logic instead.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Swap-with-tail variant for rare val.
- Remove all elements matching a predicate (in-place filter, generic).
- Move all zeros to the end while preserving order (LC 283).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
When would swap-with-tail be better?
When val is very rare; instead of copying matched slots over and over, you swap the bad element with the last unprocessed element and shrink the working range. Saves writes but only matters at scale.
Should I clear the tail?
No; the grader ignores everything past k. In production you'd truncate via `nums.length = k`.
Practice these live with InterviewChamp.AI
Drill Remove Element and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →