Leetcode: Remove element
Given an array
nums, delete all items that are equal to
val. Return the total number of remaining items and perform the deletion in place.
The solution looks as follows. Initially,
k is set to the length of the list. If we remove one element we decrement
k by one. The one thing here is that we advance the pointer
i only if we don’t remove an element. This is due to the fact that the position of the following elements change if we delete an item from the array.