Soyo

Day 1 of 30 days Leetcode challenge

Single Number

Given a non-empty array of integers, every element appears twice except for one. Find that single one.


Solution: O(n)

Using bitwise xOr: x ^ x = 0, 0 ^ x = x

class Solution {
    func singleNumber(_ nums: [Int]) -> Int {
        return nums.reduce(0, ^)
    }
}

Comments