Skip to main content

5. Remove Element

easyAsked at Palantir

Remove all occurrences of a value from an array in-place and return the new length. Palantir asks this to test the same two-pointer pattern used in filter-stage transforms of their Foundry data pipelines.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Palantir loops.

  • Glassdoor (2025-10)Reported in a Palantir junior-engineer phone screen.
  • LeetCode Discuss (2026-Q1)Palantir warm-up problem.

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 <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

Examples

Example 1

Input
nums = [3,2,2,3], val = 3
Output
2, nums = [2,2,_,_]

Example 2

Input
nums = [0,1,2,2,3,0,4,2], val = 2
Output
5, nums = [0,1,4,0,3,_,_,_]

Approaches

1. Filter to new array

Copy non-val elements to a new array. Fails the in-place requirement.

Time
O(n)
Space
O(n)
function removeElement(nums, val) {
  const out = nums.filter(x => x !== val);
  for (let i = 0; i < out.length; i++) nums[i] = out[i];
  return out.length;
}

Tradeoff: Violates the spirit of the question.

2. Two-pointer write/read

Walk read pointer; write only when nums[read] !== val.

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: Linear time, O(1) space. Mirrors a filter stage in a Foundry pipeline.

Palantir-specific tips

Palantir interviewers grade this on whether you describe the pattern as 'in-place filter' before coding. A bonus signal is mentioning the swap-with-end variant: if val is rare, swapping the last element into position is fewer writes than the sequential compaction. They want to know you've thought about which variant fits a sparse vs dense workload.

Common mistakes

  • Forgetting to increment write only when keeping the element.
  • Trying to remove elements with .splice() inside the loop — that's O(n^2).
  • Not handling the empty array.

Follow-up questions

An interviewer at Palantir may pivot to one of these next:

  • What if val occurs very rarely? (Swap-from-end pattern reduces writes.)
  • What if order must be preserved AND we want the smallest number of writes?
  • Stream variant: filter a sorted stream of records to a new sink.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

When is the swap-from-end variant better?

When val is rare. Each occurrence triggers one swap with the end and the array shrinks. The sequential pattern always touches every element.

Does the order matter?

The problem says no. If it did, the sequential compaction is the only correct answer.

Practice these live with InterviewChamp.AI

Drill Remove Element and other Palantir interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →