1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

go: add «260. Single Number III»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-05-31 12:16:00 +02:00
parent 27d260c9c8
commit c66b1879eb
Signed by: mfocko
GPG key ID: 7C47D46246790496

33
go/single-number-iii.go Normal file
View file

@ -0,0 +1,33 @@
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}
}