Bird
0
0
DSA Cprogramming~30 mins

Two Non Repeating Elements in Array Using XOR in DSA C - Build from Scratch

Choose your learning style9 modes available
Two Non Repeating Elements in Array Using XOR
📖 Scenario: Imagine you have a list of numbers where every number appears twice except for two unique numbers that appear only once. You want to find those two unique numbers efficiently.
🎯 Goal: Build a program that finds the two numbers that appear only once in an array where all other numbers appear twice, using the XOR operation.
📋 What You'll Learn
Create an integer array called arr with the exact values: 2, 4, 7, 9, 2, 4
Create an integer variable called xor_all and initialize it to 0
Use a for loop with variable i to XOR all elements of arr into xor_all
Print the two unique numbers separated by a space
💡 Why This Matters
🌍 Real World
Finding unique items in data streams or logs where duplicates are common.
💼 Career
Bitwise operations and efficient algorithms are important in embedded systems, security, and performance-critical applications.
Progress0 / 4 steps
1
Create the array with given values
Create an integer array called arr with the exact values: 2, 4, 7, 9, 2, 4.
DSA C
Hint

Use int arr[] = {2, 4, 7, 9, 2, 4}; to create the array and int n = 6; for its size.

2
Initialize XOR variable
Create an integer variable called xor_all and initialize it to 0.
DSA C
Hint

Use int xor_all = 0; to start XOR accumulation.

3
XOR all elements in the array
Use a for loop with variable i from 0 to n - 1 to XOR each element of arr into xor_all.
DSA C
Hint

Use for (int i = 0; i < n; i++) { xor_all ^= arr[i]; } to XOR all elements.

4
Find and print the two unique numbers
Complete the program to find the two unique numbers using XOR logic and print them separated by a space.
DSA C
Hint

Use the rightmost set bit of xor_all to divide elements into two groups. XOR each group separately to find the unique numbers. Then print them.