Find Duplicate Number (XOR)

beginner
25 min

Find Duplicate Number (XOR)

Welcome to CodeYourCraft! Today, we'll be diving into a fun problem-solving exercise that combines Data Structures and Algorithms. We'll learn how to find a duplicate number in an array using the XOR operation. This technique is not only interesting but also very useful in real-world programming.

What is XOR (Exclusive OR)

šŸ’” Pro Tip: XOR (Exclusive OR) is a binary operation that gives a true result only when the corresponding bits in the operands are different.

In simpler terms, if we have two numbers (let's call them A and B), the XOR operation will give a true result (1) when A and B are different, and a false result (0) when they are the same.

Here's a table showing the XOR operation for different binary numbers:

0 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0

Solving the Problem: Find Duplicate Number

Let's consider an array arr containing numbers from 1 to n with one number repeated. Our goal is to find the duplicate number.

First, let's compute the XOR of all numbers in the array:

python
result = arr[0] ^ arr[1] ^ arr[2] ^ ... ^ arr[n-1]

šŸ“ Note: In Python, the ^ operator represents the XOR operation.

Now, we will find the XOR of all numbers from 1 to n:

python
expected_result = 1 ^ 2 ^ 3 ^ ... ^ n

The duplicate number is the result of the XOR between the original array and the expected array:

python
duplicate_number = result ^ expected_result

Let's see an example to clarify this:

Example 1: Find Duplicate Number in [2, 4, 1, 2, 3, 4]

First, let's calculate the XOR of all numbers in the array:

python
arr = [2, 4, 1, 2, 3, 4] result = arr[0] ^ arr[1] ^ arr[2] ^ arr[3] ^ arr[4] ^ arr[5] result = 2 ^ 4 ^ 1 ^ 2 ^ 3 ^ 4 result = 0

Now, we'll calculate the XOR of all numbers from 1 to 6:

python
expected_result = 1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 expected_result = 0 ^ 1 ^ 1 ^ 0 ^ 1 ^ 1 expected_result = 1

Finally, we'll find the duplicate number:

python
duplicate_number = result ^ expected_result duplicate_number = 0 ^ 1 duplicate_number = 1

The duplicate number in the array is 1.

Advanced Example: Find Duplicate Number in a Sorted Array

In this case, the array is sorted, and we can take advantage of this to find the duplicate number more efficiently.

Let's consider an example:

Example 2: Find Duplicate Number in [1, 1, 3, 3, 5]

First, we'll compute the XOR of the first and last elements:

python
result = arr[0] ^ arr[-1] result = 1 ^ 5 result = 0b101 (in binary)

Next, we'll calculate the XOR of the left and right halves of the array:

python
n = len(arr) if n % 2 == 1: mid = (n + 1) // 2 else: mid = n // 2 left_xor = arr[0] ^ arr[1] ^ ... ^ arr[mid-1] right_xor = arr[mid] ^ arr[mid+1] ^ ... ^ arr[-1]

Now, we'll repeat the process with the left and right halves until we find the duplicate number.

In our example, let's say we have:

python
left_xor = 1 right_xor = 3

Since the left half contains only one element (1), we'll calculate the XOR of the right half:

python
right_xor = 3

The duplicate number is one of the numbers in the right half with an XOR equal to the XOR of the left half. In this case, the duplicate number is either 1 or 3.

Now, let's find the duplicate number by XORing each number in the right half with the XOR of the left half:

python
duplicates = [] for num in arr[mid:]: if num ^ left_xor == right_xor: duplicates.append(num)

In our example, the duplicate numbers are 1 and 3.

Quiz

Quick Quiz
Question 1 of 1

What is the result of the XOR operation between the numbers 0 and 1?

Quick Quiz
Question 1 of 1

Given the array [1, 1, 3, 3, 5], what is the duplicate number found using the XOR method?