Single Number XOR Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Single Number XOR runs in O(n) time and O(1) space, in Go as in every other language.
- ✓Go has no reduce and no functional fold in the standard library, so the idiomatic implementation is an explicit range loop with an accumulator.
- ✓Write the loop. Name the accumulator for what it holds rather than acc, use the blank identifier for the unused index (for _, n := range nums), and return the zero value naturally for an empty slice.
- ✓The Go trap: Go is the only language on this page where ^ means two different things depending on arity.
- ✓The compiler emits a single XOR instruction per iteration with a bounds check that it eliminates for the range form, so this is as fast as C for practical purposes.
What Single Number XOR actually is
Single Number XOR in Go: what you actually reach for
Go implementation
package bits
// SingleNumber returns the one value that appears an odd number of times.
// An empty slice returns 0, the identity of XOR.
func SingleNumber(nums []int) int {
result := 0
for _, n := range nums {
result ^= n
}
return result
}
// Generic over any integer width; ^ is defined for all of them.
func SingleNumberOf[T ~int | ~int32 | ~int64](nums []T) T {
var result T
for _, n := range nums {
result ^= n
}
return result
}
// Go-specific reading hazard, worth knowing before review:
// x ^ y binary XOR
// ^x unary bitwise NOT (Go has no ~ operator)
// x &^ y AND NOT: clears the bits of y in xThink Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Single Number XOR?
Go has no reduce and no functional fold in the standard library, so the idiomatic implementation is an explicit range loop with an accumulator. That is not a gap to work around, a three-line loop is what a Go reviewer expects to read. Since Go 1.21 the slices package covers sorting and searching, but there is deliberately no slices.Reduce.
What is the most common Go mistake when implementing Single Number XOR?
Go is the only language on this page where ^ means two different things depending on arity. Binary x ^ y is XOR as expected, but UNARY ^x is bitwise NOT, Go has no ~ operator at all. So a typo that drops an operand turns a XOR into a complement and still compiles cleanly. The second Go-specific trap is the AND NOT operator &^, which reads like a typo for &^ nothing and is a real operator (x &^ y clears the bits of y in x); reviewers scanning quickly routinely misread it as XOR.
How does Single Number XOR actually perform in Go?
The compiler emits a single XOR instruction per iteration with a bounds check that it eliminates for the range form, so this is as fast as C for practical purposes. Go does not auto-vectorise this loop the way LLVM does for Rust and C, so on very large slices Go will be measurably slower than the Rust version above despite identical source-level complexity, a real, checkable difference rather than a stylistic one. The function does not allocate and will inline into its caller. Time complexity is O(n) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? GitHub Copilot free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.