Bird
0
0
DSA Cprogramming~20 mins

Two Non Repeating Elements in Array Using XOR in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XOR Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of XOR-based two unique elements finder
What is the output of the following C code that finds two non-repeating elements in an array using XOR?
DSA C
#include <stdio.h>

void findTwoUnique(int arr[], int n) {
    int xor = 0;
    for (int i = 0; i < n; i++) {
        xor ^= arr[i];
    }
    int set_bit = xor & ~(xor - 1);
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] & set_bit)
            x ^= arr[i];
        else
            y ^= arr[i];
    }
    printf("%d %d\n", x, y);
}

int main() {
    int arr[] = {2, 3, 7, 9, 2, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
    findTwoUnique(arr, n);
    return 0;
}
A7 9
B9 7
C2 3
D0 0
Attempts:
2 left
💡 Hint
Focus on how XOR and set bits separate the two unique numbers.
🧠 Conceptual
intermediate
1:30remaining
Why XOR helps find two unique elements?
Why does XOR operation help in finding two non-repeating elements in an array where every other element repeats twice?
AXOR adds all numbers to find the sum of unique elements
BXOR multiplies all numbers to find product of unique elements
CXOR sorts the array to separate unique elements
DXOR of two identical numbers is zero, so duplicates cancel out leaving XOR of unique elements
Attempts:
2 left
💡 Hint
Think about what happens when you XOR a number with itself.
Predict Output
advanced
2:00remaining
Output of modified XOR code with different array
What is the output of this C code that finds two unique elements using XOR?
DSA C
#include <stdio.h>

void findTwoUnique(int arr[], int n) {
    int xor = 0;
    for (int i = 0; i < n; i++) {
        xor ^= arr[i];
    }
    int set_bit = xor & ~(xor - 1);
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] & set_bit)
            x ^= arr[i];
        else
            y ^= arr[i];
    }
    printf("%d %d\n", x, y);
}

int main() {
    int arr[] = {4, 1, 2, 1, 2, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    findTwoUnique(arr, n);
    return 0;
}
A0 0
B4 5
C5 4
D1 2
Attempts:
2 left
💡 Hint
Check which bit is set in XOR and how elements split.
🔧 Debug
advanced
1:30remaining
Identify the error in XOR two unique elements code
What error will this code produce when finding two unique elements using XOR?
DSA C
#include <stdio.h>

void findTwoUnique(int arr[], int n) {
    int xor = 0;
    for (int i = 0; i <= n; i++) {
        xor ^= arr[i];
    }
    int set_bit = xor & ~(xor - 1);
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] & set_bit)
            x ^= arr[i];
        else
            y ^= arr[i];
    }
    printf("%d %d\n", x, y);
}

int main() {
    int arr[] = {2, 3, 7, 9, 2, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
    findTwoUnique(arr, n);
    return 0;
}
ACompilation error due to missing semicolon
BSegmentation fault due to out-of-bounds array access
CLogical error printing wrong unique elements
DNo error, prints correct output
Attempts:
2 left
💡 Hint
Check the loop boundary in the first for loop.
🚀 Application
expert
2:00remaining
Number of items in result after XOR partition
Given an array where all elements except two appear twice, after XORing all elements and splitting by rightmost set bit, how many elements will be in each partition?
AOne partition has all duplicates plus one unique element; the other has the other unique element only
BBoth partitions have equal number of elements
COne partition has only duplicates; the other has both unique elements
DBoth partitions have only unique elements
Attempts:
2 left
💡 Hint
Duplicates appear twice and share bits; unique elements differ in the chosen bit.