Two Non Repeating Elements in Array Using XOR in DSA Go - Time & Space Complexity
We want to understand how the time needed to find two unique numbers in an array grows as the array gets bigger.
Specifically, how many steps does the XOR method take when the input size increases?
Analyze the time complexity of the following code snippet.
func findTwoUnique(arr []int) (int, int) {
xor := 0
for _, num := range arr {
xor ^= num
}
setBit := xor & (-xor)
x, y := 0, 0
for _, num := range arr {
if num&setBit != 0 {
x ^= num
} else {
y ^= num
}
}
return x, y
}
This code finds two numbers that appear only once in an array where all other numbers appear twice, using XOR operations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops over the array.
- How many times: Each loop runs once over all elements, so twice in total.
Each element is processed twice, once in each loop, so the total steps grow directly with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 steps |
| 100 | About 200 steps |
| 1000 | About 2000 steps |
Pattern observation: The steps grow linearly as the input size grows.
Time Complexity: O(n)
This means the time to find the two unique numbers grows in direct proportion to the size of the input array.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested, so their times add up, not multiply. This keeps the complexity linear, O(n).
Understanding how XOR helps find unique elements efficiently shows your skill in using bitwise operations and analyzing linear time solutions, a valuable skill in many coding challenges.
"What if the array had three unique elements instead of two? How would the time complexity change?"
Practice
Solution
Step 1: Understand XOR properties
XOR of a number with itself is 0, and XOR with 0 is the number itself.Step 2: Apply XOR to all elements
XORing all elements cancels out duplicates, leaving XOR of two unique elements.Final Answer:
XOR of all elements gives XOR of the two unique elements -> Option AQuick Check:
XOR cancels duplicates, leaving unique pair XOR [OK]
- Thinking XOR sorts or counts elements
- Assuming XOR gives unique elements directly
- Confusing XOR with addition or subtraction
Solution
Step 1: Identify correct XOR initialization
XOR operation starts with 0 because XOR with 0 returns the number itself.Step 2: Check Go syntax for variable initialization
"xor := 0" correctly initializes an integer variable for XOR operations.Final Answer:
xor := 0 -> Option BQuick Check:
Start XOR with 0 for correct accumulation [OK]
- Using 1 instead of 0 for XOR start
- Trying to initialize XOR with nil or slice
- Incorrect Go syntax for variable declaration
Solution
Step 1: XOR all elements in the array
Calculate XOR stepwise: 0^2=2, 2^3=1, 1^7=6, 6^9=15, 15^2=13, 13^3=14, 14^9=7, 7^11=12.Step 2: Verify final XOR value
The final XOR value after processing all elements is 12.Final Answer:
12 -> Option AQuick Check:
XOR of all elements = 12 [OK]
- Miscomputing XOR stepwise
- Assuming XOR sums elements
- Ignoring duplicates canceling out
Solution
Step 1: Check loop boundary
Loop uses i <= len(arr), which causes out-of-range error because valid indices are 0 to len(arr)-1.Step 2: Correct loop condition
Change to i < len(arr) to avoid index out of range and correctly XOR all elements.Final Answer:
Loop condition should be i < len(arr), not i <= len(arr) -> Option CQuick Check:
Index out of range fixed by correct loop boundary [OK]
- Using <= instead of < in loop condition
- Initializing XOR with wrong value
- Misunderstanding XOR usage for strings
Solution
Step 1: XOR all elements to get xorVal
Calculate XOR: 6^3=5, 5^5=0, 0^4=4, 4^5=1, 1^3=2, 2^4=6, 6^7=1. Final xorVal = 1.Step 2: Find rightmost set bit and separate elements
Rightmost set bit in xorVal=1. Partition array into two groups based on this bit and XOR separately to find unique elements 6 and 7.Final Answer:
[6, 7] -> Option DQuick Check:
Unique elements are 6 and 7 after XOR partition [OK]
- Not partitioning array by set bit
- Mixing up duplicates with unique elements
- Incorrect XOR calculations
Ready to prove it?
Pass the test and this pattern moves unknown
