Intuition
To remove all occurrences of a value from an array in-place, we can minimize the number of writes by swapping unwanted elements with the last element. This way, we avoid shifting elements multiple times and only move each element at most once.
Approach
We use two pointers:
istarts from the beginning and scans the array.jstarts from the end and represents the new length of the array.
While i < j:
- If
nums[i]equalsval, we swap it withnums[j-1](the last unchecked element) and decreasejby 1. We do not incrementiin this case, because the swapped-in element atineeds to be checked. - If
nums[i]does not equalval, we incrementi.
At the end, i is the count of elements not equal to val.
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
from typing import List
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
i = 0
j = len(nums)
# Loop until i reaches j
while i < j:
if nums[i] == val:
# Swap with the last unchecked element
nums[i] = nums[j - 1]
j -= 1
else:
i += 1
# i is the new length of the array without val
return i