Leetcode 27: Remove Element

A two-pointer approach to remove a specific element in-place from an array

Two-pointer approach

Initialization

Main loop

Termination

My solution

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        save = 0
        for read in range(len(nums)):
            if nums[read] != val:
                nums[save] = nums[read]
                save += 1
        return save

Complexity Analysis