Swift Solution - 15ms


  • 0
    Z
    class Solution {
        func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
           
            guard !nums.isEmpty else {
                return 0
            }
            
            var index = 0
            
            while index < nums.count {
                let currentNum = nums[index]
                if currentNum == val {
                    nums.remove(at: index)
                } else {
                    index += 1
                }
            }
            
            return nums.count
            
        }
    }
    

Log in to reply
 

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