5. Remove Element
easyAsked at FigmaGiven an array and a value, remove all instances of that value in place. Figma uses this as a quick check on in-place pointer manipulation — relevant when their renderer prunes deleted nodes from a flat batch before re-sending to the GPU.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Figma loops.
- LeetCode Discuss (2025)— Easy bucket in Figma OA.
- Glassdoor (2026-Q1)— Phone screen, paired with array compaction tasks.
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 which are not equal to val. The first k elements of nums should hold those elements.
Constraints
0 <= nums.length <= 1000 <= nums[i] <= 500 <= val <= 100
Examples
Example 1
nums = [3,2,2,3], val = 3k = 2, nums = [2,2,_,_]Example 2
nums = [0,1,2,2,3,0,4,2], val = 2k = 5, nums = [0,1,4,0,3,_,_,_]Approaches
1. Filter into new array
Build a new array of non-val elements; 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: Works, but doubles memory for what should be an in-place operation.
2. Two pointers (write index)
Walk with read; advance write only when nums[read] !== val. Order preserved.
- 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];
}
}
return write;
}Tradeoff: Single pass, in-place. Same skeleton as Remove Duplicates.
Figma-specific tips
Figma will appreciate that you reach for the same two-pointer pattern as the dedup problem — they treat that as evidence you've internalized 'in-place compact with write-index.' Mention that if order didn't matter, you could swap-with-end for fewer writes; Figma engineers like seeing the variant noted aloud.
Common mistakes
- Returning the array instead of the count.
- Decrementing nums.length manually — irrelevant; only the prefix matters.
- Using splice in a loop — O(n^2) because splice shifts elements.
Follow-up questions
An interviewer at Figma may pivot to one of these next:
- Move Zeroes (LC 283) — same idea, val is 0 and order must be preserved.
- Swap-with-end variant (order-insensitive) — fewer writes when val is rare.
- Apply to a doubly linked list — change pointer rewiring.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Does order need to be preserved?
The problem allows any order. The two-pointer scan preserves it for free, which is fine. Swap-with-end doesn't preserve order but minimizes writes.
When is swap-with-end better?
When val is rare. You only swap once per occurrence of val, vs the scan which writes every kept element.