Two Non Repeating Elements in Array Using XOR in DSA Java - Time & Space Complexity
We want to understand how the time needed grows when finding two unique numbers in an array where every other number repeats.
How does the number of steps change as the array gets bigger?
Analyze the time complexity of the following code snippet.
public static void findTwoNonRepeating(int[] arr) {
int xor = 0;
for (int num : arr) {
xor ^= num;
}
int setBit = xor & (-xor);
int x = 0, y = 0;
for (int num : arr) {
if ((num & setBit) != 0) x ^= num;
else y ^= num;
}
System.out.println(x + " " + y);
}
This code finds two numbers that appear only once in the array by using XOR operations.
- 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 checked 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 steps double the input size but still grow in a straight line.
Time Complexity: O(n)
This means the time needed grows directly with the number of elements in the array.
[X] Wrong: "Because there are two loops, the time is O(n²)."
[OK] Correct: The loops run one after another, not inside each other, so their times add up, not multiply.
Understanding this helps you explain how to efficiently find unique elements using bit operations, a useful skill in many coding problems.
"What if we used nested loops to find the two unique elements instead? How would the time complexity change?"
Practice
Solution
Step 1: XOR all elements in the array
When we XOR all elements, pairs cancel out because x ^ x = 0, leaving XOR of the two unique elements.Step 2: Use the XOR result to separate unique elements
The XOR result has set bits where the two unique elements differ, helping to divide elements into two groups.Final Answer:
XOR of all elements gives XOR of the two unique elements -> Option DQuick Check:
XOR all = unique1 ^ unique2 [OK]
- Thinking XOR counts frequency
- Assuming XOR sorts elements
- Using sum instead of XOR
arr?Solution
Step 1: Initialize XOR to zero and XOR all elements
Starting with zero ensures XOR accumulates correctly. Using a for-each loop with ^= operator applies XOR to each element.Step 2: Check correctness of other options
int xor = 1; for(int num : arr) xor += num; uses addition, not XOR. int xor = arr[0]; for(int i = 0; i < arr.length; i++) xor ^= arr[i]; incorrectly loops from i=0 after initializing with arr[0], XORing arr[0] twice which cancels it out. int xor = 0; for(int i = 0; i < arr.length; i++) xor = xor + arr[i]; uses addition, not XOR.Final Answer:
int xor = 0; for(int num : arr) xor ^= num; -> Option BQuick Check:
Initialize xor=0 and XOR all elements [OK]
- Using addition instead of XOR
- Starting XOR with non-zero value
- Using wrong loop syntax
int[] arr = {2, 3, 7, 9, 2, 3};
int xor = 0;
for (int num : arr) xor ^= num;
int setBit = xor & (-xor);
int x = 0, y = 0;
for (int num : arr) {
if ((num & setBit) != 0) x ^= num;
else y ^= num;
}
System.out.println(x + ", " + y);Solution
Step 1: XOR all elements to get xor = 7 ^ 9 = 14
Pairs (2,2) and (3,3) cancel out, leaving xor = 7 ^ 9 = 14.Step 2: Find rightmost set bit and separate elements
setBit = 14 & (-14) = 2. Elements with bit 2 set: 2, 2, 3, 3, 7, 9 split into groups. XOR group 1: 7, XOR group 2: 9.Final Answer:
7, 9 -> Option AQuick Check:
Unique elements are 7 and 9 [OK]
- Mixing order of output
- Not using rightmost set bit correctly
- Confusing pairs with unique elements
int[] arr = {4, 1, 2, 1, 2, 5};
int xor = 0;
for (int i = 0; i <= arr.length; i++) {
xor ^= arr[i];
}
int setBit = xor & (-xor);
int x = 0, y = 0;
for (int num : arr) {
if ((num & setBit) != 0) x ^= num;
else y ^= num;
}
System.out.println(x + ", " + y);Solution
Step 1: Check loop boundary in XOR calculation
The for loop uses i <= arr.length, which causes ArrayIndexOutOfBoundsException because valid indices are 0 to arr.length-1.Step 2: Correct loop condition to i < arr.length
Changing to i < arr.length fixes the error and allows proper XOR of all elements.Final Answer:
Loop condition should be i < arr.length, not i <= arr.length -> Option AQuick Check:
Array index must be within bounds [OK]
- Using wrong loop boundary causing runtime error
- Incorrect XOR initialization
- Misunderstanding setBit calculation
arr = {10, 20, 10, 30, 40, 20, 50, 40}, which Java code snippet correctly finds the two non-repeating elements using XOR and prints them in ascending order?Solution
Step 1: XOR all elements and find rightmost set bit
XOR of all elements gives xor = 30 ^ 50 = 44. setBit = 44 & (-44) = 4.Step 2: Separate elements into two groups and XOR each group
Elements with bit 4 set XOR to 50, others XOR to 30.Step 3: Print the two unique elements in ascending order
Using Math.min and Math.max prints "30, 50" as required.Final Answer:
Prints 30, 50 in ascending order -> Option CQuick Check:
Use XOR groups and sort output [OK]
- Using bitwise OR instead of AND for setBit
- Swapping group conditions incorrectly
- Printing sum instead of elements
Ready to prove it?
Pass the test and this pattern moves unknown
