What if you could find the odd one out instantly without checking every pair?
Why Find the Only Non Repeating Element Using XOR in DSA Typescript?
Imagine you have a box of socks where every sock has a matching pair except one. You want to find that single sock without checking each pair one by one.
Manually checking each sock against all others takes a lot of time and can easily lead to mistakes, especially if the box is large. It's slow and tiring to compare every item multiple times.
Using XOR, you can quickly find the single sock without comparing pairs. XOR cancels out pairs automatically, leaving only the unique sock at the end.
for (let i = 0; i < arr.length; i++) { let count = 0; for (let j = 0; j < arr.length; j++) { if (arr[i] === arr[j]) count++; } if (count === 1) return arr[i]; }
let result = 0; for (const num of arr) { result ^= num; } return result;
This method lets you find the unique item in a list instantly, no matter how big the list is.
Finding the one defective product in a batch where every other product is identical and perfect.
Manual pair checking is slow and error-prone.
XOR operation cancels out duplicates automatically.
Efficiently finds the unique element in linear time.
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 XOR to example
For example, 5 XOR 5 = 0.Final Answer:
It returns 0 -> Option CQuick Check:
X XOR X = 0 [OK]
- Thinking XOR returns the number itself
- Confusing XOR with OR or AND
- Assuming XOR sums the numbers
Solution
Step 1: Identify correct initial value for XOR
For XOR operations, starting with 0 is correct because 0 XOR any number returns that number.Step 2: Check code snippets
Onlylet result = 0;correctly initializes for XOR accumulation.Final Answer:
let result = 0; -> Option BQuick Check:
Initial XOR value = 0 [OK]
- Starting XOR with 1 instead of 0
- Using null or undefined which cause errors
- Not initializing the variable
const arr = [2, 3, 2, 4, 3];
let result = 0;
for (const num of arr) {
result ^= num;
}
console.log(result);Solution
Step 1: XOR all elements in the array
Calculate stepwise: 0 XOR 2 = 2, 2 XOR 3 = 1, 1 XOR 2 = 3, 3 XOR 4 = 7, 7 XOR 3 = 4.Step 2: Identify the non-repeating element
All repeating numbers cancel out, leaving 4 as the only non-repeating element.Final Answer:
4 -> Option DQuick Check:
Result after XOR = 4 [OK]
- Not XORing all elements
- Confusing output with last element
- Assuming sum instead of XOR
const arr = [1, 2, 1, 3, 2];
let result;
for (let i = 0; i < arr.length; i++) {
result ^= arr[i];
}
console.log(result);Solution
Step 1: Check variable initialization
The variable 'result' is declared but not initialized, so it is undefined initially.Step 2: Understand XOR with undefined
XOR with undefined causes NaN or runtime error; it must start at 0.Final Answer:
Variable 'result' is not initialized -> Option AQuick Check:
Initialize result = 0 before XOR loop [OK]
- Forgetting to initialize result
- Assuming undefined XOR number works
- Incorrect loop boundaries
const arr = [5, 5, 5, 7, 7, 8, 8];
Solution
Step 1: Understand XOR behavior with odd counts
XOR cancels even appearances (like twice) to 0, but leaves the result of odd appearances (like thrice: x ^ x ^ x = 0, not x).Step 2: Analyze the given array
5 appears thrice (odd), 7 and 8 twice (even); XOR of all yields 0, so XOR alone cannot find the unique element repeated thrice.Final Answer:
No, XOR fails because it only cancels pairs, not triples -> Option AQuick Check:
XOR fails to find element repeated thrice [OK]
- Thinking XOR works for triples
- Believing sorting is required for XOR
- Confusing XOR with requiring exactly one unique
Ready to prove it?
Pass the test and this pattern moves unknown
