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:- Modify
numsso that the firstkelements contain values not equal toval. - The remaining elements and the array size do not matter.
- Return
k.
Example 1
Input: nums = [2,2,2], val=2
Output: 0
Explanation: All elements equal 2; after removal nothing remains — length 0.
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.
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.
Output: 2
Explanation: Remove all occurrences of 3: [3,2,2,3] -> [2,2], new length 2.