Skip to content
Onsite

Remove Element

EasyP2Coding

Remove every occurrence of val from nums in place, keeping the remaining elements in their original order, using O(1) extra memory. After the call the first k positions of nums must hold the k kept elements.

Return those first k elements as an array (so the answer is the kept elements and k is its length).

Example 1

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

Example 2

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

Example 3

Input
nums = [], val = 1
Output
[]

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 10.