Simple Python solution in O(n)


  • 0
    R

    """
    def removeElement(self, nums, val):
    count = 0
    #push all the values that are not vaal to front and pop the remaining

        for i in range(len(nums)):
            if nums[i] != val:
                nums[count],nums[i] = nums[i],nums[count]
                count+=1
        for i in range(count,len(nums)):
            nums.pop()
        return len(nums)
    

    """


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.