Remove Element

Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in-place. The relative order of the elements may be changed. Return the new length after removal.

Input Format

nums = array of integers, val = value to remove

Output Format

new length of array after removal

Constraints

Examples

Example 1:

Input:

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

Output:

2

Explanation:

After removal, array could be [2,2] and length 2.

Example 2:

Input:

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

Output:

5

Explanation:

After removal, one possible array is [0,1,3,0,4] and length 5.

Example 3:

Input:

nums = [1,1,1]
val = 1

Output:

0

Explanation:

All elements are removed, length 0.

Loading...
Remove Element - Two Pointers DSA Problem