Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def removeElement(self, nums: List[int], val: int) -> int:
- """
- Understanding the Logic:
- We use two pointers:
- i → Iterates through all elements in the array.
- k → Tracks the position where the next valid element should be placed.
- k represents the new length of the array after removal. Tracks the
- position where the next valid element should be written.
- So effectively, is just the slow pointer of tracker of all non-target
- elements
- """
- k = 0
- for i in range(len(nums)):
- if nums[i] != val:
- nums[k] = nums[i]
- k += 1
- return k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement