Remove Element
Easy · two pointers
You are given a list of integers nums and an integer val. Return a new list that holds every number from nums that is not equal to val, in the same order as they appear in nums.
If every number equals val, or nums is empty, return an empty list.
Examples
Example 1
Input: nums = [3, 1, 3, 2, 3, 4], val = 3
Output: [1, 2, 4]
The three 3s are dropped. The other numbers keep their original order.
Example 2
Input: nums = [5, 5, 5], val = 5
Output: []
Every number equals val, so nothing is left.
Example 3
Input: nums = [7, 8], val = 9
Output: [7, 8]
No number equals 9, so the list is unchanged.
Constraints
- 0 <= len(nums) <= 2000
- -1000 <= nums[i] <= 1000
- -1000 <= val <= 1000