LeetCode/go/single-number-iii.go
Matej Focko d2ef757754
go: allow testing
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-06-16 11:44:08 +02:00

33 lines
410 B
Go

package main
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}
}