23. Remove Element

Given an integer array nums and an integer val. Remove all occurrences of val from nums in place. Then return the number of elements not equal to val.
Let that count be k. For the solution to be accepted:
  1. Modify nums so that the first k elements contain values not equal to val.
  2. The remaining elements and the array size do not matter.
  3. Return k.
Example 1
Input: nums = [2,2,2], val=2
Output: 0
Explanation: All elements equal 2; after removal nothing remains — length 0.
Example 2
Input: nums = [0,1,2,3,4], val=5
Output: 5
Explanation: The value 5 is not in the array, length remains 5.
Example 3
Input: nums = [3,2,2,3], val=3
Output: 2
Explanation: Remove all occurrences of 3: [3,2,2,3] -> [2,2], new length 2.
arrayseasy
JavaScript
Loading...
Line 1, Char 1