Lonely Integer

  • + 0 comments

    Swift:

    func lonelyinteger(a: [Int]) -> Int {
        var dict = [Int: Bool]()
        
        a.forEach { value in
            // will nil the dictionary entry if more than 1 occurrence
            if dict[value] != nil {
                dict[value] = nil
            } else {
                dict[value] = true
            }
        }
        
        // should only be 1 entry left
        return dict.keys.first ?? -1
    }