A two-pointer approach to remove a specific element in-place from an array
read to read the new elementsave to save the non-val elementval by moving the non-val elements to the front.val instead of duplicates.read starts from 0 to iterate over every element in the array.save starts from 0, tracking where the next non-val item should go.read proceeds by a for loop.nums[read] is different from val.val, we just increment read.nums[save] and increment save by 1.save only proceeds when we find a valid element, ensuring all non-val elements are packed at the front of the array and we always have save <= read.read reaches the end of the array (stop condition).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
read and save).