Discover how a simple XOR trick can save you hours of tedious searching!
Why Two Non Repeating Elements in Array Using XOR in DSA C++?
Imagine you have a big box of socks where every sock has a matching pair except two socks that are unique. You want to find those two unique socks quickly without checking every single sock against all others.
Checking each sock against all others one by one takes a lot of time and effort, especially if the box is huge. It's easy to make mistakes or miss pairs when doing this manually.
Using XOR, a special math trick, you can find the two unique socks in just one pass through the box. XOR helps cancel out all the pairs automatically, leaving only the unique socks.
for (int i = 0; i < n; i++) { bool found_pair = false; for (int j = 0; j < n; j++) { if (i != j && arr[i] == arr[j]) { found_pair = true; break; } } if (!found_pair) { // unique element found } }
int xor_all = 0; for (int num : arr) { xor_all ^= num; } // xor_all now has XOR of two unique elements
This method lets you find two unique elements in a large list quickly and without extra memory, making your program fast and efficient.
Finding two unique defective products in a large batch where every other product is perfectly paired, without checking each product multiple times.
Manual pair checking is slow and error-prone.
XOR cancels out pairs automatically.
Efficiently finds two unique elements in one pass.
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
