📖 Scenario: Imagine you have a list of numbers where every number appears twice except for one number that appears only once. You want to find that unique number quickly.
🎯 Goal: Build a TypeScript program that finds the only number in an array that does not repeat, using the XOR operation.
📋 What You'll Learn
Create an array called numbers with the exact values: [2, 3, 5, 4, 5, 3, 4]
Create a variable called unique and set it to 0
Use a for loop with variable num to iterate over numbers and update unique by XORing it with num
Print the value of unique using console.log
💡 Why This Matters
🌍 Real World
Finding unique elements quickly is useful in error detection, data cleaning, and network packet analysis.
💼 Career
Understanding bitwise operations like XOR helps in systems programming, embedded systems, and performance-critical applications.
Progress0 / 4 steps
1
Create the array of numbers
Create an array called numbers with these exact values: [2, 3, 5, 4, 5, 3, 4]
DSA Typescript
Hint
Use const to create the array numbers with the given values.
2
Initialize the unique variable
Create a variable called unique and set it to 0
DSA Typescript
Hint
Use let to create a variable unique and assign it 0.
3
Use XOR to find the unique number
Use a for loop with variable num to iterate over numbers and update unique by XORing it with num inside the loop
DSA Typescript
Hint
Use for (const num of numbers) to loop and update unique with unique ^= num.
4
Print the unique number
Print the value of unique using console.log(unique)
DSA Typescript
Hint
Use console.log(unique) to show the unique number.
Practice
(1/5)
1. What does the XOR operation do when applied between two identical numbers?
easy
A. It returns 1
B. It returns the number itself
C. It returns 0
D. It returns the sum of the numbers
Solution
Step 1: Understand XOR property for identical numbers
XOR of a number with itself always results in 0 because bits cancel out.
Step 2: Apply XOR to example
For example, 5 XOR 5 = 0.
Final Answer:
It returns 0 -> Option C
Quick Check:
X XOR X = 0 [OK]
Hint: XOR of same numbers is always zero [OK]
Common Mistakes:
Thinking XOR returns the number itself
Confusing XOR with OR or AND
Assuming XOR sums the numbers
2. Which of the following TypeScript code snippets correctly initializes a variable to zero for XOR operation?
easy
A. let result = 1;
B. let result = 0;
C. let result = null;
D. let result = undefined;
Solution
Step 1: Identify correct initial value for XOR
For XOR operations, starting with 0 is correct because 0 XOR any number returns that number.
Step 2: Check code snippets
Only let result = 0; correctly initializes for XOR accumulation.
Final Answer:
let result = 0; -> Option B
Quick Check:
Initial XOR value = 0 [OK]
Hint: Start XOR with zero to keep numbers unchanged [OK]
Common Mistakes:
Starting XOR with 1 instead of 0
Using null or undefined which cause errors
Not initializing the variable
3. What is the output of the following TypeScript code?
const arr = [2, 3, 2, 4, 3];
let result = 0;
for (const num of arr) {
result ^= num;
}
console.log(result);