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 code that finds the only non-repeating element using XOR?
Consider the following C# code that uses XOR to find the unique element in an array where every other element repeats twice. What will be printed?
DSA C#
using System; class Program { static void Main() { int[] arr = {2, 3, 5, 4, 5, 3, 4}; int result = 0; foreach (int num in arr) { result ^= num; } Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
XOR of a number with itself is 0, and XOR with 0 is the number itself.
✗ Incorrect
All numbers except one appear twice. XOR cancels out pairs, leaving the unique number.
❓ Predict Output
intermediateWhat is the output of this XOR-based unique element finder with negative numbers?
What will this C# program print when the array contains negative and positive integers with one unique element?
DSA C#
using System; class Program { static void Main() { int[] arr = {-1, -2, -2, -1, -3}; int unique = 0; foreach (int n in arr) { unique ^= n; } Console.WriteLine(unique); } }
Attempts:
2 left
💡 Hint
XOR works the same with negative numbers as with positive.
✗ Incorrect
Pairs cancel out, leaving the unique number -3.
🧠 Conceptual
advancedWhy does XOR help find the only non-repeating element in an array?
Which of the following best explains why XOR operation helps find the unique element in an array where every other element repeats twice?
Attempts:
2 left
💡 Hint
Think about what happens when you XOR a number twice.
✗ Incorrect
XOR of a number with itself is zero, so pairs cancel out, leaving only the unique number.
❓ Predict Output
advancedWhat is the output of this code with multiple unique elements?
What will this C# program print if the array contains two unique elements and others repeated twice?
DSA C#
using System; class Program { static void Main() { int[] arr = {1, 2, 3, 2, 1, 4}; int result = 0; foreach (int num in arr) { result ^= num; } Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
XOR of two unique numbers is their XOR value, not one of them.
✗ Incorrect
XOR of 3 and 4 is 7, so the output is 7.
🔧 Debug
expertWhat error does this code produce when finding the unique element using XOR?
This C# code attempts to find the unique element using XOR but causes an error. What error will it produce?
DSA C#
using System; class Program { static void Main() { int[] arr = {1, 2, 1, 3, 2}; int result; foreach (int num in arr) { result ^= num; } Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Check if 'result' is initialized before use.
✗ Incorrect
Variable 'result' is declared but not initialized before XOR operation, causing a compile error.
