Bird
Raised Fist0
DSA Javascriptprogramming~5 mins

Two Non Repeating Elements in Array Using XOR in DSA Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does XOR operation do when applied between two same numbers?
XOR of two same numbers is 0 because each bit cancels out. For example, 5 ^ 5 = 0.
Click to reveal answer
beginner
Why XOR all elements in the array to find two non-repeating elements?
XORing all elements cancels out pairs of repeating numbers, leaving XOR of the two unique numbers.
Click to reveal answer
intermediate
How do you find a bit that differs between two unique numbers after XOR?
Find the rightmost set bit (bit with value 1) in the XOR result using 'xor & (-xor)'.
Click to reveal answer
intermediate
How does dividing elements into two groups help find the two unique numbers?
Elements are split by whether they have the set bit or not. Each group XORs to one unique number.
Click to reveal answer
beginner
What is the time complexity of finding two non-repeating elements using XOR?
The time complexity is O(n) because the array is traversed a few times linearly.
Click to reveal answer
What is the XOR of a number with 0?
AThe number itself
B0
C1
DUndefined
After XORing all elements in the array, what does the result represent?
ANumber of unique elements
BSum of all elements
CProduct of unique elements
DXOR of two unique elements
How do you find the rightmost set bit in a number 'x'?
Ax & (-x)
Bx & (x - 1)
Cx | (-x)
D~x
Why split elements into two groups based on a set bit?
ATo sort the array
BTo find the sum of elements
CTo separate unique elements
DTo count duplicates
What is the space complexity of this XOR method?
AO(log n)
BO(1)
CO(n)
DO(n^2)
Explain step-by-step how XOR helps find two non-repeating elements in an array where all others repeat twice.
Think about how pairs cancel out and how the set bit helps separate unique numbers.
You got /4 concepts.
    Describe why the rightmost set bit in the XOR of two unique numbers is important for solving the problem.
    Focus on how this bit helps separate the two unique numbers.
    You got /3 concepts.

      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 cancel out all repeating elements and isolate the unique ones
      B. To sort the array in ascending order
      C. To count the total number of elements in the array
      D. To reverse the array elements

      Solution

      1. Step 1: Understand XOR behavior with duplicates

        XOR of two identical numbers is 0, so repeating elements cancel out.
      2. Step 2: Isolate unique elements using XOR

        XOR of all elements leaves XOR of two unique elements, isolating them.
      3. Final Answer:

        To cancel out all repeating elements and isolate the unique ones -> Option A
      4. Quick Check:

        XOR cancels duplicates = isolate unique elements [OK]
      Hint: XOR cancels pairs, leaving only unique elements [OK]
      Common Mistakes:
      • Thinking XOR sorts the array
      • Using XOR to count elements
      • Assuming XOR reverses arrays
      2. Which of the following JavaScript code snippets correctly initializes the XOR variable to find two non-repeating elements?
      easy
      A. let xor = null;
      B. let xor = 1;
      C. let xor = 0;
      D. let xor = undefined;

      Solution

      1. Step 1: Understand XOR identity element

        XOR with 0 returns the other number unchanged, so start with 0.
      2. Step 2: Initialize XOR variable correctly

        Initialize xor as 0 to accumulate XOR of all elements properly.
      3. Final Answer:

        let xor = 0; -> Option C
      4. Quick Check:

        Start XOR with 0 for correct accumulation [OK]
      Hint: Initialize XOR with 0, not 1 or null [OK]
      Common Mistakes:
      • Starting XOR with 1 changes results
      • Using null or undefined causes errors
      • Not initializing XOR variable
      3. What will be the output of the following JavaScript code?
      const arr = [2, 3, 7, 9, 2, 3];
      let xor = 0;
      for (const num of arr) {
        xor ^= num;
      }
      console.log(xor);
      medium
      A. 7
      B. 0
      C. 9
      D. 14

      Solution

      1. Step 1: XOR all elements in the array

        Calculate XOR stepwise: 0^2=2, 2^3=1, 1^7=6, 6^9=15, 15^2=13, 13^3=14.
      2. Step 2: Final XOR value

        The final XOR value after processing all elements is 14.
      3. Final Answer:

        14 -> Option D
      4. Quick Check:

        XOR all elements = 14 [OK]
      Hint: XOR duplicates cancel; XOR unique pairs remain [OK]
      Common Mistakes:
      • Miscomputing XOR stepwise
      • Assuming output is one unique element
      • Confusing XOR with sum
      4. Identify the error in the following code snippet that attempts to find two non-repeating elements using XOR:
      const arr = [4, 1, 2, 1, 2, 5];
      let xor = 0;
      for (let i = 0; i <= arr.length; i++) {
        xor ^= arr[i];
      }
      console.log(xor);
      medium
      A. Use let xor = null instead of 0
      B. Loop condition should be i < arr.length, not i <= arr.length
      C. Array elements should be sorted before XOR
      D. XOR should start from 1, not 0

      Solution

      1. Step 1: Check loop boundary condition

        The loop runs from i=0 to i<=arr.length, which accesses arr[arr.length] (undefined).
      2. Step 2: Correct loop condition

        Change condition to i < arr.length to avoid accessing undefined and runtime error.
      3. Final Answer:

        Loop condition should be i < arr.length, not i <= arr.length -> Option B
      4. Quick Check:

        Loop must stay within array bounds [OK]
      Hint: Use i < arr.length to avoid out-of-bounds errors [OK]
      Common Mistakes:
      • Using <= instead of < in loop
      • Starting XOR with 1 instead of 0
      • Sorting array unnecessarily
      5. Given the array [6, 1, 3, 6, 1, 5], which two numbers are the non-repeating elements found using the XOR method? Choose the correct pair.
      hard
      A. [3, 5]
      B. [6, 1]
      C. [1, 3]
      D. [5, 6]

      Solution

      1. Step 1: XOR all elements to get xor of two unique numbers

        6^1=7, 7^3=4, 4^6=2, 2^1=3, 3^5=6 (xor = 6).
      2. Step 2: Find rightmost set bit and separate elements

        Rightmost set bit in 6 (binary 110) is bit 2 (value 2). Separate elements into two groups based on this bit and XOR each group to find unique numbers 3 and 5.
      3. Final Answer:

        [3, 5] -> Option A
      4. Quick Check:

        Unique elements are 3 and 5 [OK]
      Hint: Use rightmost set bit to split and find unique numbers [OK]
      Common Mistakes:
      • Mixing up repeating and unique elements
      • Not splitting array by set bit
      • Incorrect XOR calculations
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard