algos_and_structures/leetcode/static_arrays/0027_remove_element.py

23 lines
537 B
Python

# Link: https://leetcode.com/problems/remove-element/description/
from typing import List
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
c = 0
n = len(nums) - 1
while n >= 0:
if nums[n] == val:
del nums[n]
else:
c += 1
n -= 1
return c
if __name__ == "__main__":
# nums = [0, 1, 2, 2, 3, 0, 4, 2]
nums = [3, 2, 2, 3]
print(nums)
print(Solution().removeElement(nums, 3))
print(nums)