Share a Swift solution.


  • 0
    class Solution {
    func removeDuplicates(inout nums: [Int]) -> Int {
    var firstIndex = 0
    var compIndex = 1
    var lastIndex = nums.count - 1
    if nums.isEmpty {
        return 0
    }else if nums.count <= 2{
        return nums.count
    }else {
        while firstIndex != lastIndex {
            if compIndex == lastIndex {
                return nums.count
            }else {
                if nums[firstIndex] == nums[compIndex] {
                    if nums[compIndex] == nums[compIndex+1] {
                        while nums[compIndex] == nums[compIndex+1] && nums[firstIndex] == nums[compIndex] {
                            if compIndex+1 == lastIndex {
                                nums.removeAtIndex(compIndex+1)
                                return nums.count
                            }
                            nums.removeAtIndex(compIndex+1)
                            lastIndex -= 1
                        }
                    }else {
                        firstIndex += 1
                        compIndex += 1
                    }
                }else {
                    firstIndex += 1
                    compIndex += 1
                }
            }
        }
        return nums.count
    }
    }
    }

Log in to reply
 

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