Given a non-empty array of integers nums
, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Given a non-empty array of integers nums
, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
func singleNumber(nums []int) int {
count := make(map[int]int, (len(nums)/2)+1)
result := 0
for _, n := range nums {
count[n]++
c := count[n]
if c == 1 {
result += n
} else {
result -= n
}
}
return result
}