Very simple Swift solution with Sets


  • 0
    A
    
    class Solution {
        func singleNumber(_ nums: [Int]) -> Int {
            var nset = Set(nums)
            var nset2 = nset
            for n in nums {
                if nset.contains(n) {
                    nset.remove(n)
                } else {
                    nset2.remove(n)
                }
            }
            return nset2.first!
        }
    }

Log in to reply
 

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