Remove Element

EasyTwo Pointerstwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array nums and an integer val. Remove every occurrence of val and keep the remaining elements in the same relative order they had in nums. Return the compacted array: the elements of nums that are not equal to val, in their original order. If every element equals val (or nums is empty), return []. In a real interview this is posed as an in-place compaction using O(1) extra space: write the kept elements into the front of nums with a write pointer, then return that prefix.

Examples

Input: nums = [3,2,2,3], val = 3
Output: [2,2]
Both 3s are removed; the two 2s remain in order.
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: [0,1,3,0,4]
All three 2s are dropped and the other elements keep their order.

Constraints

  • 0 <= nums.length <= 100
  • 0 <= nums[i] <= 50
  • 0 <= val <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems