Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
class Solution {
func singleNumber(var nums: [Int]) -> Int {
//if nums.count == 1 { return nums[0] }
for i in 1..<nums.count {
nums[0] ^= nums[i]
}
return nums[0]
}
}
well why i didn't check num.count == 1
, it says an array of integerS. "S".
So i don't think it's necessary for me to check it.
and for ^: a ^ b = b ^ a, a ^ a = 0, a ^ 0 = a.