9 lines Swift O(n) solution


  • 0
    Z
       func removeDuplicates(_ nums: inout [Int]) -> Int {
            var len = 0
            for i in 0 ..< nums.count {
                if (i == 0 || nums[i] != nums[i - 1]) {
                    nums[len] = nums[i]
                    len += 1
                }
            }
            return len
        }
    
    

Log in to reply
 

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