5. Remove Element
easyAsked at Riot GamesFilter an array in place by value — Riot uses this to test write-pointer mechanics before anti-cheat blacklist filtering questions.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an integer array and a value val, remove all occurrences of val in place and return the new length. Order of the remaining elements may change.
Constraints
0 <= nums.length <= 1000 <= nums[i], val <= 50
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=25Approaches
1. Filter into new array
Build a new array of survivors then copy back.
- Time
- O(n)
- Space
- O(n)
const keep = nums.filter(x=>x!==val);
for (let i=0;i<keep.length;i++) nums[i]=keep[i];
return keep.length;Tradeoff:
2. Two-pointer overwrite
Advance a write index only for survivors.
- Time
- O(n)
- Space
- O(1)
function removeElement(nums, val) {
let w = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] !== val) nums[w++] = nums[i];
}
return w;
}Tradeoff:
Riot Games-specific tips
Riot anti-cheat filters player events with the same write-pointer technique to drop banned actors without reshuffling the broadcast buffer mid server tick.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Remove Element and other Riot Games interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →