mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
33 lines
423 B
Go
33 lines
423 B
Go
package single_number_iii
|
|
|
|
func singleNumber(nums []int) []int {
|
|
reduce := func() int {
|
|
xor := 0
|
|
for _, x := range nums {
|
|
xor ^= x
|
|
}
|
|
return xor
|
|
}
|
|
xor := reduce()
|
|
|
|
findMask := func() int {
|
|
mask := 1
|
|
for (xor & mask) == 0 {
|
|
mask <<= 1
|
|
}
|
|
return mask
|
|
}
|
|
mask := findMask()
|
|
|
|
a := 0
|
|
b := 0
|
|
for _, x := range nums {
|
|
if (x & mask) != 0 {
|
|
a ^= x
|
|
} else {
|
|
b ^= x
|
|
}
|
|
}
|
|
|
|
return []int{a, b}
|
|
}
|