📖 Scenario: Imagine you have a list of product IDs scanned at a store. Every product ID appears twice except one product that was scanned only once by mistake. You want to find that unique product ID quickly.
🎯 Goal: You will write a program to find the only product ID that does not repeat in the list using the XOR operation.
📋 What You'll Learn
Create an array called productIDs with the exact values: [4, 1, 2, 1, 2]
Create a variable called uniqueID and set it to 0
Use a for loop with variable id to iterate over productIDs and update uniqueID by XORing it with id
Print the value of uniqueID to display the only non-repeating product ID
💡 Why This Matters
🌍 Real World
Stores and inventory systems often need to find unique or missing items quickly from large lists.
💼 Career
Understanding bitwise operations like XOR helps in optimizing algorithms for performance-critical applications.
Progress0 / 4 steps
1
Create the product ID list
Create an array called productIDs with these exact values: [4, 1, 2, 1, 2]
DSA Javascript
Hint
Use const productIDs = [4, 1, 2, 1, 2]; to create the array.
2
Initialize the unique ID variable
Create a variable called uniqueID and set it to 0
DSA Javascript
Hint
Use let uniqueID = 0; to start with zero.
3
Use XOR to find the unique product ID
Use a for loop with variable id to iterate over productIDs and update uniqueID by XORing it with id
DSA Javascript
Hint
Use for (const id of productIDs) { uniqueID ^= id; } to XOR all IDs.
4
Print the unique product ID
Print the value of uniqueID to display the only non-repeating product ID
DSA Javascript
Hint
Use console.log(uniqueID); to print the result.
Practice
(1/5)
1. What does the XOR operation do when applied between two identical numbers?
easy
A. It returns 0
B. It returns the number itself
C. It returns 1
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 ^ 5 = 0 as each bit is the same and cancels.
Final Answer:
It returns 0 -> Option A
Quick Check:
X ^ X = 0 [OK]
Hint: XOR same numbers always zeroes out [OK]
Common Mistakes:
Thinking XOR returns the number itself
Confusing XOR with OR operation
Assuming XOR sums numbers
2. Which of the following is the correct syntax to XOR two variables a and b in JavaScript?
easy
A. a && b
B. a ^ b
C. a xor b
D. a | b
Solution
Step 1: Recall JavaScript bitwise XOR operator
In JavaScript, the XOR operator is represented by the caret symbol ^.
Step 2: Check each option
a ^ b uses ^ correctly. a xor b uses 'xor' which is not valid syntax. Options A and D are logical AND and bitwise OR respectively.
Final Answer:
a ^ b -> Option B
Quick Check:
Use ^ for XOR in JavaScript [OK]
Hint: Use ^ symbol for XOR in JavaScript [OK]
Common Mistakes:
Using 'xor' keyword
Confusing XOR with && or ||
Using | instead of ^
3. What will be the output of the following code?
const arr = [2, 3, 2, 4, 3];
let result = 0;
for (const num of arr) {
result ^= num;
}
console.log(result);
medium
A. 2
B. 0
C. 4
D. 3
Solution
Step 1: XOR all elements in the array
Start with result = 0, then XOR with each element: 0 ^ 2 = 2, 2 ^ 3 = 1, 1 ^ 2 = 3, 3 ^ 4 = 7, 7 ^ 3 = 4.
Step 2: Final XOR result is the non-repeating element
The only element that does not repeat is 4, which is the final XOR result.
Final Answer:
4 -> Option C
Quick Check:
Final XOR = 4 [OK]
Hint: XOR all numbers; result is unique element [OK]
Common Mistakes:
Not starting XOR from 0
XORing incorrectly in loop
Assuming sum instead of XOR
4. Identify the error in the following code that tries to find the non-repeating element using XOR:
const arr = [1, 2, 1, 3, 2];
let result;
for (let i = 0; i < arr.length; i++) {
result ^= arr[i];
}
console.log(result);
medium
A. Variable 'result' is not initialized to 0
B. Loop condition should be i <= arr.length
C. XOR operator is incorrect, should be &&
D. Array should be sorted before XOR
Solution
Step 1: Check initialization of 'result'
The variable 'result' is declared but not initialized, so it is undefined initially.
Step 2: Effect of uninitialized 'result' on XOR
XOR with undefined results in NaN, so the final output will be incorrect. Initializing 'result' to 0 fixes this.
Final Answer:
Variable 'result' is not initialized to 0 -> Option A
Quick Check:
Initialize result = 0 before XOR loop [OK]
Hint: Always initialize XOR accumulator to 0 [OK]
Common Mistakes:
Not initializing XOR variable
Using wrong loop condition
Misusing XOR operator
5. Given an array where every element appears three times except one element which appears once, can XOR be used directly to find the unique element? Why or why not?
hard
A. Yes, but only if the array length is odd
B. Yes, XOR works for any number of repetitions
C. No, because XOR requires sorted arrays
D. No, because XOR only cancels pairs of identical numbers
Solution
Step 1: Understand XOR behavior with repeated elements
XOR cancels out pairs of identical numbers because x ^ x = 0.
Step 2: Analyze effect of triply repeated elements
Elements repeated thrice do not cancel out with XOR because XOR of three identical numbers is the number itself, not zero.
Step 3: Conclusion on XOR usage
Therefore, XOR alone cannot find the unique element if others repeat thrice; other methods are needed.
Final Answer:
No, because XOR only cancels pairs of identical numbers -> Option D
Quick Check:
XOR cancels pairs, not triples [OK]
Hint: XOR cancels pairs only, not triples [OK]
Common Mistakes:
Assuming XOR works for any repetition count
Thinking sorting helps XOR
Confusing XOR with sum or count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.