136. Single Number
[Easy] “Given a non-empty array of integers, every element appears twice except for one. Find that single one in, O(n)."
Python3:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
saved = {}
for num in nums:
if not saved.get(num):
saved[num] = 1
else:
del saved[num]
return list(saved.keys())[0]
Go:
func singleNumber(nums []int) int {
seen := make(map[int]int)
var res int
for _, num := range nums {
if seen[num] == 0 {
seen[num] = 1
} else {
delete(seen, num)
}
}
for key, _ := range seen {
res = key
}
return res
}