Find the Only Non Repeating Element Using XOR in DSA Go - Time & Space Complexity
We want to understand how fast we can find the single element that does not repeat in a list using XOR.
How does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
func findNonRepeating(arr []int) int {
result := 0
for _, num := range arr {
result ^= num
}
return result
}
This code finds the only number that appears once while all others appear twice by using XOR.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: XOR operation inside a single loop over the array.
- How many times: Once for each element in the array (n times).
Each element is processed once, so the total work grows directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 XOR operations |
| 100 | 100 XOR operations |
| 1000 | 1000 XOR operations |
Pattern observation: The operations increase linearly as the input size grows.
Time Complexity: O(n)
This means the time to find the unique element grows in direct proportion to the number of elements.
[X] Wrong: "Using XOR means the time complexity is constant because XOR is a simple operation."
[OK] Correct: Even though XOR itself is fast, it must be done for every element, so the total time depends on the number of elements.
Understanding this linear time approach shows you can solve problems efficiently by using bitwise operations and simple loops.
"What if the array contains multiple elements that appear only once? How would the time complexity change?"
Practice
Solution
Step 1: Understand XOR property for identical numbers
XOR of a number with itself always results in 0 because bits cancel out.Step 2: Apply this property to the question
Since the two identical numbers cancel out, the result is 0.Final Answer:
It returns 0 -> Option BQuick Check:
n XOR n = 0 [OK]
- Thinking XOR returns the number itself
- Confusing XOR with OR operation
- Assuming XOR sums numbers
nums?Solution
Step 1: Identify the correct type for XOR accumulation
XOR operates on integers, so the variable must be an integer initialized to 0.Step 2: Check each option
var result int = 0 initializes an integer variable to 0, suitable for XOR operations. Others use wrong types or data structures.Final Answer:
var result int = 0 -> Option AQuick Check:
Integer zero initialization for XOR [OK]
- Using slice or map instead of int
- Initializing with empty string
- Not initializing variable
package main
import "fmt"
func main() {
nums := []int{2, 3, 2, 4, 3}
result := 0
for _, num := range nums {
result ^= num
}
fmt.Println(result)
}Solution
Step 1: XOR all elements in the slice
Calculate XOR stepwise: 0^2=2, 2^3=1, 1^2=3, 3^4=7, 7^3=4.Step 2: Identify the non-repeating element
The final XOR result is 4, which is the only number appearing once.Final Answer:
4 -> Option CQuick Check:
XOR cancels duplicates, leaves 4 [OK]
- Adding numbers instead of XOR
- Miscomputing XOR steps
- Ignoring duplicates
package main
import "fmt"
func main() {
nums := []int{5, 1, 5, 2, 1}
var result int
for i := 0; i <= len(nums); i++ {
result ^= nums[i]
}
fmt.Println(result)
}Solution
Step 1: Check loop boundary condition
The loop runs from i=0 to i<=len(nums), which causes out-of-range access at i=len(nums).Step 2: Correct the loop condition
Change condition to i < len(nums) to avoid index out of range error.Final Answer:
Loop condition should be i < len(nums), not i <= len(nums) -> Option AQuick Check:
Index out of range fixed by loop condition [OK]
- Using <= instead of <
- Initializing result incorrectly
- Misunderstanding XOR operator
nums := []int{7, 9, 7, 8, 9, 10, 10}. What is the unique element?Solution
Step 1: XOR all elements in the array
Calculate XOR stepwise: 0^7=7, 7^9=14, 14^7=9, 9^8=1, 1^9=8, 8^10=2, 2^10=8.Step 2: Identify the unique element
The final XOR result is 8, which appears only once in the array.Final Answer:
8 -> Option DQuick Check:
Duplicates cancel, unique is 8 [OK]
- Adding numbers instead of XOR
- Forgetting to initialize result to zero
- Misreading array elements
Ready to prove it?
Pass the test and this pattern moves unknown
