MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / singleNumber

Function singleNumber

single_number_136/solution.go:16–22  ·  view source on GitHub ↗

NOTE: bitwise XOR arithmetic operator used to find single digit n XOR 0 = n n XOR n = 0 So, for [2,2,1]: a = 0 ^ 2 = 2 a = 2 ^ 2 = 0 a = 0 ^ 1 = 1 = answer [2,1,2] a = 0 ^ 2 = 2 a = 2 ^ 1 = 3 (because 00000010 XOR 000000001 -> 00000011 -> 3) a = 3 ^ 2 = 1 (because 00000011 XOR 00000010 -> 00000001

(nums []int)

Source from the content-addressed store, hash-verified

14// a = 2 ^ 1 = 3 (because 00000010 XOR 000000001 -> 00000011 -> 3)
15// a = 3 ^ 2 = 1 (because 00000011 XOR 00000010 -> 00000001 -> 1)
16func singleNumber(nums []int) int {
17 a := 0
18 for i := 0; i < len(nums); i++ {
19 a ^= nums[i]
20 }
21 return a
22}
23
24func singleNumber2(nums []int) int {
25 seen := make(map[int]bool)

Callers 1

Test_singleNumberFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_singleNumberFunction · 0.68