Challenge - 5 Problems
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this XOR-based function?
Given the array [2, 3, 5, 4, 5, 3, 4], what will the function return?
DSA Typescript
function findUnique(arr: number[]): number {
let result = 0;
for (const num of arr) {
result ^= num;
}
return result;
}
console.log(findUnique([2, 3, 5, 4, 5, 3, 4]));Attempts:
2 left
💡 Hint
XOR of a number with itself is zero, and XOR with zero is the number itself.
✗ Incorrect
The function uses XOR to cancel out all numbers that appear twice. Only the unique number remains.
Here, 5, 3, and 4 appear twice, so they cancel out. The remaining number is 2.
❓ Predict Output
intermediateWhat is the output of this code with negative numbers?
What will the function return for the array [-1, -2, -2, -1, -3]?
DSA Typescript
function findUnique(arr: number[]): number {
let result = 0;
for (const num of arr) {
result ^= num;
}
return result;
}
console.log(findUnique([-1, -2, -2, -1, -3]));Attempts:
2 left
💡 Hint
XOR works with negative numbers as well because it operates on binary bits.
✗ Incorrect
Pairs of -1 and -2 cancel out. The only number left is -3.
🔧 Debug
advancedWhat error does this code produce?
Identify the error when running this code snippet.
DSA Typescript
function findUnique(arr: number[]): number {
let result = 0;
for (let i = 0; i < arr.length; i++) {
result ^= arr[i];
}
return result;
}
console.log(findUnique([1, 2, 2, 1, 3]));Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
No error occurs. The loop runs from i = 0 to i = arr.length exclusive, so no out-of-bounds access happens.
The output is 3 as expected.
🧠 Conceptual
advancedWhy does XOR help find the unique element?
Which property of XOR makes it useful to find the only non-repeating element in an array?
Attempts:
2 left
💡 Hint
Think about what happens when you XOR a number twice.
✗ Incorrect
XOR cancels out pairs because n ^ n = 0, and 0 ^ n = n.
This property helps isolate the unique number.
🚀 Application
expertWhat is the output after XOR operations on this array?
Given the array [10, 14, 10, 14, 9, 9, 7], what is the unique element found by XOR?
DSA Typescript
function findUnique(arr: number[]): number {
let result = 0;
for (const num of arr) {
result ^= num;
}
return result;
}
console.log(findUnique([10, 14, 10, 14, 9, 9, 7]));Attempts:
2 left
💡 Hint
All numbers except one appear twice.
✗ Incorrect
Pairs 10, 14, and 9 cancel out. The remaining unique number is 7.
