📖 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 quickly without using extra space.
🎯 Goal: Build a TypeScript program that finds the two non-repeating elements in an array using the XOR operation.
📋 What You'll Learn
Create an array called numbers with the exact values: [2, 3, 7, 9, 2, 3, 9, 11]
Create a variable called xorAll and initialize it to 0
Use a for loop with variable num to XOR all elements in numbers and store the result in xorAll
Find the rightmost set bit in xorAll and store it in a variable called rightmostSetBit
Create two variables num1 and num2 initialized to 0
Use a for loop with variable num to separate numbers into two groups based on rightmostSetBit and XOR each group to find the two unique numbers
Print the two unique numbers num1 and num2 separated by a space
💡 Why This Matters
🌍 Real World
Finding unique items in large datasets quickly, such as detecting unique error codes or unique user IDs in logs.
💼 Career
This technique is useful for software engineers working on performance-critical applications, embedded systems, or data processing pipelines.
Progress0 / 5 steps
1
Create the array of numbers
Create an array called numbers with these exact values: [2, 3, 7, 9, 2, 3, 9, 11]
DSA Typescript
Hint
Use const numbers = [2, 3, 7, 9, 2, 3, 9, 11]; to create the array.
2
Initialize XOR accumulator
Create a variable called xorAll and set it to 0
DSA Typescript
Hint
Use let xorAll = 0; to start XOR accumulation.
3
XOR all elements to find combined XOR
Use a for loop with variable num to XOR all elements in numbers and store the result in xorAll
DSA Typescript
Hint
Use for (const num of numbers) and inside the loop do xorAll ^= num;
4
Find rightmost set bit and separate groups
Create a variable called rightmostSetBit and set it to xorAll & (-xorAll). Then create variables num1 and num2 initialized to 0. Use a for loop with variable num to XOR numbers into num1 if (num & rightmostSetBit) !== 0, else XOR into num2
DSA Typescript
Hint
Use const rightmostSetBit = xorAll & (-xorAll); to find the bit. Then separate numbers using a loop and XOR into num1 and num2.
5
Print the two unique numbers
Write a console.log statement to print num1 and num2 separated by a space
DSA Typescript
Hint
Use console.log(num1 + " " + num2); to print the two unique numbers separated by a space.
Practice
(1/5)
1. What is the main purpose of using XOR operation to find two non-repeating elements in an array?
easy
A. To sort the array in ascending order
B. To count the frequency of each element
C. To cancel out all repeating elements and isolate the unique ones
D. To reverse the array elements
Solution
Step 1: Understand XOR properties
XOR of two same numbers is 0, and XOR with 0 is the number itself.
Step 2: Apply XOR on array elements
XORing all elements cancels out repeating ones, leaving XOR of two unique elements.
Final Answer:
To cancel out all repeating elements and isolate the unique ones -> Option C
Quick Check:
XOR cancels duplicates = isolate unique elements [OK]
Hint: XOR cancels pairs, leaving unique elements [OK]
Common Mistakes:
Thinking XOR sorts the array
Using XOR to count frequencies
Assuming XOR reverses elements
2. Which of the following TypeScript code snippets correctly initializes the XOR variable to start finding two unique elements?
easy
A. let xor = 1;
B. let xor = 0;
C. let xor = -1;
D. let xor = null;
Solution
Step 1: Identify initial XOR value
XOR with 0 returns the number itself, so start with 0.
Step 2: Confirm initialization
Initializing xor as 0 allows proper XOR accumulation over array elements.
Final Answer:
let xor = 0; -> Option B
Quick Check:
Start XOR with 0 for correct accumulation [OK]
Hint: Initialize XOR with 0 to accumulate correctly [OK]
Common Mistakes:
Starting XOR with 1 changes results
Using null or undefined causes errors
Starting XOR with -1 gives wrong output
3. What will be the output of the following TypeScript code?
const arr = [2, 3, 7, 9, 2, 3];
let xor = 0;
for (const num of arr) {
xor ^= num;
}
console.log(xor);