My Simple and Fast Swift Solution(48ms)


  • 0
    Y
    func singleNumber(_ nums: [Int]) -> [Int] {
            var sorted = nums.sorted(by: <)
            var i = 0
            var j = 0
            while i+2 < nums.count{
                if i == 0 && sorted[i] != sorted[i+1]{  //left border
                    sorted[j] = sorted[i]
                    j+=1
                }else if i+2 == nums.count-1 && sorted[i+1] != sorted[i+2]{  //right border
                    sorted[j] = sorted[i+2]
                    j+=1
                }
                if sorted[i] != sorted[i+1] && sorted[i+1] != sorted[i+2]{
                    sorted[j] = sorted[i+1]
                    j+=1
                }
                i+=1
            }
            return [sorted[0], sorted[1]]
    }

Log in to reply
 

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