5. Remove Element
easyAsked at AsanaRemove all instances of a value from an array in-place. Asana asks this to test whether you understand the two-pointer write/read separation that scales to their bulk-task-mutation pipelines.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Asana loops.
- Glassdoor (2026-Q1)— Asana phone-screen warmup.
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.
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 and copy
Build a new array with .filter, then write back.
- Time
- O(n)
- Space
- O(n)
function removeElement(nums, val) {
const keep = nums.filter(x => x !== val);
for (let i = 0; i < keep.length; i++) nums[i] = keep[i];
return keep.length;
}Tradeoff: O(n) extra space. Asana wants the in-place version.
2. Two-pointer in-place
Write pointer advances only on non-val elements.
- Time
- O(n)
- Space
- O(1)
function removeElement(nums, val) {
let w = 0;
for (let r = 0; r < nums.length; r++) {
if (nums[r] !== val) nums[w++] = nums[r];
}
return w;
}Tradeoff: O(1) space. If most elements equal val, swap-from-end is faster — fewer writes.
Asana-specific tips
Asana cares about the variation: if val is rare, swap-from-end minimizes writes; if val is common, the standard two-pointer wins. Mention both and pick based on the expected distribution — interviewers love that you think about real workloads.
Common mistakes
- Splicing in a loop — O(n^2) and shifts indices unpredictably.
- Returning the array length instead of the count of kept elements.
- Off-by-one in the write pointer.
Follow-up questions
An interviewer at Asana may pivot to one of these next:
- What if order doesn't matter — can you do fewer writes?
- Remove multiple values at once.
- Apply to a linked list instead of an array.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is splice() bad here?
Each splice is O(n) and the indices shift, so you'd either skip elements or process some twice. Two pointers avoids both issues.
When is swap-from-end better?
When val is rare. With swap-from-end, each removal does one write; with the standard approach, every keep does one write.
Practice these live with InterviewChamp.AI
Drill Remove Element and other Asana interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →