Two Non Repeating Elements in Array Using XOR in DSA C++ - Time & Space Complexity
We want to understand how the time needed grows when finding two unique numbers in an array where all others repeat.
How does the number of steps change as the array gets bigger?
Analyze the time complexity of the following code snippet.
int xorAll = 0;
for (int num : arr) {
xorAll ^= num;
}
int setBit = xorAll & (-xorAll);
int x = 0, y = 0;
for (int num : arr) {
if (num & setBit) x ^= num;
else y ^= num;
}
// x and y are the two unique numbers
This code finds two numbers that appear only once in an array where every other number appears 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, 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 work doubles the input size, so it grows linearly.
Time Complexity: O(n)
This means the time to find the two unique numbers grows directly in proportion to the array size.
[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.
Understanding this linear time approach shows you can solve problems efficiently without extra space, a skill valued in many coding challenges.
"What if the array had three unique numbers 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 repeating elements, leaving XOR of two unique elements.Final Answer:
XOR of all elements gives XOR of the two unique elements -> Option CQuick 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
arr of size n?Solution
Step 1: Initialize XOR with 0
Start with 0 because XOR with 0 returns the number itself.Step 2: Loop through array correctly
Loop from 0 to n-1 to cover all elements; use XOR assignment operator ^=.Final Answer:
int xorVal = 0; for(int i = 0; i < n; i++) xorVal ^= arr[i]; -> Option BQuick Check:
Correct loop and XOR initialization [OK]
- Starting loop from 1 and missing first element
- Using addition instead of XOR
- Initializing XOR with 1 instead of 0
- Looping out of array bounds
int arr[] = {2, 3, 7, 9, 2, 3};
int n = 6;
int xorVal = 0;
for(int i = 0; i < n; i++) {
xorVal ^= arr[i];
}
std::cout << xorVal << std::endl;Solution
Step 1: XOR all elements
Calculate XOR stepwise: 0^2=2, 2^3=1, 1^7=6, 6^9=15, 15^2=13, 13^3=14.Step 2: Result is XOR of two unique elements
Since 2 and 3 repeat, XOR leaves 7 and 9 combined as 14.Final Answer:
14 -> Option AQuick Check:
XOR of unique pair (7^9) = 14 [OK]
- Calculating XOR incorrectly stepwise
- Assuming output is one unique element
- Confusing XOR with sum
int xorVal = 0;
for(int i = 0; i <= n; i++) {
xorVal ^= arr[i];
}Solution
Step 1: Check loop bounds
Loop runs from i=0 to i<=n, which means i goes up to n, exceeding array index (0 to n-1).Step 2: Consequence of out-of-bounds access
Accessing arr[n] causes undefined behavior or runtime error.Final Answer:
Loop runs out of array bounds causing runtime error -> Option AQuick Check:
Array index must be less than n [OK]
- Using <= instead of < in loop condition
- Initializing xorVal incorrectly
- Starting loop from wrong index
arr = {4, 1, 2, 1, 2, 5}, which of the following steps correctly finds the two non-repeating elements using XOR?Solution
Step 1: XOR all elements
XOR(4^1^2^1^2^5) = 4 ^ (1^1) ^ (2^2) ^ 5 = 4 ^ 0 ^ 0 ^ 5 = 1.Step 2: Find rightmost set bit of XOR result
Rightmost set bit of 1 is 1 (binary 0001).Step 3: Partition array by this bit and XOR separately
Group elements with this bit set and not set, XOR each group to find unique elements 4 and 5.Final Answer:
XOR all elements to get 1, find rightmost set bit, partition array by this bit, XOR partitions separately -> Option DQuick Check:
Partitioning by set bit isolates unique elements [OK]
- Skipping partition step after XOR
- Using sorting instead of XOR
- Confusing addition with XOR
Ready to prove it?
Pass the test and this pattern moves unknown
