EasyArraysAI interview only
Remove Element
Asked atamazonmicrosoftapplegoogleadobe
01 · Problem
You are given an integer array nums and an integer val. Delete every occurrence of val from nums.
Return the remaining elements in the same relative order as they appeared in the input. If every element equals val (or nums is empty), return an empty array []. Try to do it in place, using O(1) extra memory.
02 · Examples
Example 01
Input
nums = [3,2,2,3], val = 3
Output
[2,2]
Both 3s are removed; the two 2s stay in order.
Example 02
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 five values keep their original order.
Example 03
Input
nums = [5,6], val = 7
Output
[5,6]
7 never appears, so the array is unchanged.
03 · Constraints
- 010 <= nums.length <= 100
- 020 <= nums[i] <= 50
- 030 <= val <= 100
04 · Optimal complexity
- Time
- O(n)
- Space
- O(1)
05 · Two ways to work on it
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.