Skip to main content

Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Problems53 / 152
Sign in

Remove Element

EASYArrayTwo Pointers

Given an integer array nums and an integer val, remove all occurrences of val in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Example 1

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

Example 2

Input:  nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5
Show hint

Use two pointers: one to iterate, another to track position for non-val elements. Copy non-val elements forward.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
nums =
[3,2,2,3]
val =
3
Expected
2
Run with your own input (JSON)