Remove Element

Given an array and a value, remove all instances of that value in-place and return the new length. A sorted-style adjacent group scan works well when matching values are easy to compress.

Input Format

nums = array of integers, val = value to remove

Output Format

new length after removal

Constraints

  • 0 <= nums.length <= 10^5; -10^4 <= nums[i], val <= 10^4

Examples

Example 1:

Input:

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

Output:

2

Explanation:

The remaining prefix can be [2,2].

Example 2:

Input:

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

Output:

5

Explanation:

All 2s are removed and the remaining values are compacted.

Loading...
Remove Element - Sorting Based Array Problems