Challenge - 5 Problems
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of XOR-based two non-repeating elements finder
What is the output of the following Java code that finds two non-repeating elements in an array using XOR?
DSA Java
public class Solution { 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 + " -> null"); } public static void main(String[] args) { int[] arr = {2, 3, 7, 9, 2, 3}; findTwoNonRepeating(arr); } }
Attempts:
2 left
💡 Hint
Focus on how XOR helps isolate the two unique numbers and how the set bit divides the array.
✗ Incorrect
The XOR of all elements gives xor = 7 ^ 9. The rightmost set bit in xor is used to separate numbers into two groups. XORing each group gives the two unique numbers 7 and 9. The order printed is 7 first then 9.
❓ Predict Output
intermediateOutput of XOR-based two non-repeating elements finder with different input
What is the output of the following Java code that finds two non-repeating elements in an array using XOR?
DSA Java
public class Solution { 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 + " -> null"); } public static void main(String[] args) { int[] arr = {4, 1, 2, 1, 2, 5}; findTwoNonRepeating(arr); } }
Attempts:
2 left
💡 Hint
Check how the set bit divides the array into two groups and XOR each group.
✗ Incorrect
The XOR of all elements is 4 ^ 5. The rightmost set bit separates numbers into two groups. XORing each group yields 5 and 4. The order printed is 5 then 4.
🔧 Debug
advancedIdentify the error in XOR two non-repeating elements code
What error does the following Java code produce when trying to find two non-repeating elements using XOR?
DSA Java
public class Solution { public static void findTwoNonRepeating(int[] arr) { int xor = 0; for (int num : arr) { xor ^= num; } int setBit = xor & (xor - 1); int x = 0, y = 0; for (int num : arr) { if ((num & setBit) != 0) { x ^= num; } else { y ^= num; } } System.out.println(x + " -> " + y + " -> null"); } public static void main(String[] args) { int[] arr = {2, 3, 7, 9, 2, 3}; findTwoNonRepeating(arr); } }
Attempts:
2 left
💡 Hint
Check how setBit is calculated using xor & -xor vs xor & xor-1.
✗ Incorrect
The expression xor & xor-1 clears the rightmost set bit instead of isolating it. This causes incorrect grouping and wrong output.
🧠 Conceptual
advancedWhy XOR helps find two unique elements in array?
Why does XOR operation help in finding two non-repeating elements in an array where every other element repeats twice?
Attempts:
2 left
💡 Hint
Recall properties of XOR with identical numbers.
✗ Incorrect
XOR of a number with itself is zero, so pairs cancel out. XOR of all elements leaves XOR of two unique elements.
❓ Predict Output
expertOutput of XOR two non-repeating elements with negative numbers
What is the output of the following Java code that finds two non-repeating elements in an array containing negative numbers using XOR?
DSA Java
public class Solution { 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 + " -> null"); } public static void main(String[] args) { int[] arr = {-1, -2, -1, -3, -2, -4}; findTwoNonRepeating(arr); } }
Attempts:
2 left
💡 Hint
XOR works the same with negative numbers; check the rightmost set bit carefully.
✗ Incorrect
XOR of all elements isolates -3 and -4 as unique. The rightmost set bit divides them correctly. Output is '-3 -> -4 -> null'.
