← switch

27. Remove Element

easy

Array · Two Pointers

read the problem on leetcode →

first solved 31 Aug 2023 · 3 accepted submissions

python
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        k=0
        for i in nums:
            if i != val:
                nums[k]=i 
                k+=1
        return k
python
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        uni=0
        for i in range(len(nums)):
            if nums[i]!=val:
                nums[uni]=nums[i]
                uni+=1
            else:
                pass
        return uni
        
python
class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        k=0
        for i in nums:
            if i!=val:
                nums[k]=i
                k+=1
        return k