5. Remove Element
easyAsked at PayPalRemove all occurrences of a value from an array in-place and return the new length. PayPal uses this to test the same two-pointer reflex they need when filtering reversed transactions out of a daily settlement batch.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in PayPal loops.
- Glassdoor (2026-Q1)— PayPal new-grad coding screen, in-place required.
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 <= 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 new array
Use Array.filter to keep elements != val.
- Time
- O(n)
- Space
- O(n)
function removeElement(nums, val) {
const filtered = nums.filter(x => x !== val);
for (let i = 0; i < filtered.length; i++) nums[i] = filtered[i];
return filtered.length;
}Tradeoff: Violates the in-place constraint. Wastes O(n) space.
2. Two-pointer write-in-place (optimal)
Write pointer k tracks the next slot. Read pointer i scans. If nums[i] != val, copy to nums[k] and advance k.
- Time
- O(n)
- Space
- O(1)
function removeElement(nums, val) {
let k = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] !== val) {
nums[k++] = nums[i];
}
}
return k;
}Tradeoff: O(1) space, single pass. Same pattern as PayPal's stream-filter for reversed transactions in settlement files.
PayPal-specific tips
PayPal cares about the in-place idiom — verify you don't allocate. Bonus signal: mention you'd swap-with-last when the order doesn't matter and val is rare (fewer writes); otherwise the two-pointer copy is cleaner.
Common mistakes
- Using splice inside the loop — O(n) per call makes the whole thing O(n^2).
- Returning the array instead of the length.
- Modifying nums.length explicitly — unnecessary and disallowed in some grader configs.
Follow-up questions
An interviewer at PayPal may pivot to one of these next:
- What if you want to preserve original order and val is rare — can you minimize writes?
- Remove all occurrences of multiple values.
- What if the array is a linked list?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Two-pointer vs swap-with-last?
Two-pointer keeps order; swap-with-last doesn't but uses fewer writes when val is rare. The problem allows reordering, so either is acceptable.
Why return the length?
In-place mutation means the array still has trailing 'junk' bytes. The length tells the caller where the valid portion ends — same idiom as a settlement-file truncate offset.
Practice these live with InterviewChamp.AI
Drill Remove Element and other PayPal interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →