Skip to main content

27. Remove Element

https://leetcode.com/problems/remove-element

Python

Hard to understand, but since the rest of elements in array are not matters Simply use the dual cur alg will work

class Solution:
def removeElement(self, nums, val: int) -> int:
max_index = len(nums) - 1
i = 0
while i <= max_index:
if nums[i] == val:
nums[i], nums[max_index] = nums[max_index], nums[i]
max_index -= 1
continue
i += 1
return i