5. Remove Element
easyAsked at SalesforceRemove all occurrences of a value from an array in place. Salesforce uses this to test in-place array manipulation, the same pattern they use in their record-filtering pipelines.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Asked on Salesforce intern phone screens as a two-pointer warmup.
- LeetCode Discuss (2025-09)— Common precursor before in-place dedup follow-ups.
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 elements of nums should hold the final result.
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
Build a new array of elements not equal to val, copy back.
- 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: Not truly in-place. Salesforce will dock you for the O(n) extra space.
2. Two-pointer overwrite
Maintain a write pointer. Walk with a read pointer; copy non-val elements forward.
- 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];
write++;
}
}
return write;
}Tradeoff: O(1) space, single pass. Same pattern as LC 26 but the comparison is against val rather than a previous element.
Salesforce-specific tips
Salesforce values you spotting this is the same two-pointer pattern as LC 26 — they specifically want you to verbalize that recognition. Bonus signal: mention the swap-from-end variant for cases where order doesn't matter, which is asymptotically the same but does fewer writes in the common case.
Common mistakes
- Calling nums.splice in a loop — turns O(n) into O(n^2) because splice shifts elements.
- Returning the array instead of the count — the contract is the count.
- Not handling the empty array case — works by default with this code, but worth stating.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- What if you want to preserve order? (You already do here.)
- What if order doesn't matter — can you reduce writes? (Swap with last and shrink length.)
- Remove all elements matching a predicate, not just a single value.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
What's the difference between this and LC 26?
LC 26 dedups consecutive equals (relies on sortedness). LC 27 removes a specific value (no sortedness needed). The two-pointer template is otherwise identical.
When would the swap-from-end variant be better?
When val appears rarely — say one in a thousand elements. Then most iterations do zero work because the swap-and-shrink only triggers on matches.
Practice these live with InterviewChamp.AI
Drill Remove Element and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →