Simple Python solution in O(n)

  • 0

    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]
        for i in range(count,len(nums)):
        return len(nums)


Log in to reply

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