Leetcode 27 - Remove element

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Note: The memory address of array is consecutive. So we have to splice the neighbors of deleted elements. It’s different from linked list.

Example 1:

1
2
3
4
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
let fastIndex = 0;
let slowIndex = 0;
while (fastIndex < nums.length) {
while (nums[fastIndex] === val) {
fastIndex++;
}
if (fastIndex >= nums.length) {
return slowIndex;
}
nums[slowIndex] = nums[fastIndex];
slowIndex++;
fastIndex++;
}
return slowIndex;
};