Remove Element

Easy~10 min

Given an array of integers nums and an integer val, return a new array with all occurrences of val removed. The relative order of the remaining elements should be preserved.

For example, given nums = [3, 2, 2, 3] and val = 3, return [2, 2].

Examples

Example 1
Input: nums = [3, 2, 2, 3], val = 3
Output: [2, 2]
Explanation: Both occurrences of 3 are removed.
Example 2
Input: nums = [0, 1, 2, 2, 3, 0, 4, 2], val = 2
Output: [0, 1, 3, 0, 4]
Explanation: All three 2s are removed; order of remaining elements is preserved.
Example 3
Input: nums = [1, 1, 1], val = 1
Output: []
Explanation: All elements match val, so the result is empty.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= val <= 10^9
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run