Topic describes

The fast pointer goes through the array and assigns the items in the array that are not equal to the value of val to the positions where the index is the slow pointer. Convert to code:

export function removeElement(nums: number[], val: number) :number {
  let fast = 0;
  let slow = 0;

  while (fast < nums.length) {
    if(nums[fast] ! == val) { nums[slow] = nums[fast]; slow++; } fast++; }return slow;
}

Copy the code