Given a limited range array of size n containing elements between 1 and n-1 with one element repeating, find the duplicate number in it without using any extra space.

For example,

Input:  { 1, 2, 3, 4, 4 }
Output: The duplicate element is 4
 
 
Input:  { 1, 2, 3, 4, 2 }
Output: The duplicate element is 2

Practice this problem

Approach 1: Using Hashing

The idea is to use hashing to solve this problem. We can use a visited boolean array to mark if an element is seen before or not. If the element is already encountered before, the visited array will return true.

Following is the implementation in C++, Java, and Python based on the above idea:

C++


Download  Run Code

Output:

The duplicate element is 4

Java


Download  Run Code

Output:

The duplicate element is 4

Python


Download  Run Code

Output:

The duplicate element is 4

The time complexity of the above solution is O(n) and requires O(n) extra space, where n is the size of the input.

Approach 2: Using Array Indices

We can solve this problem in constant space. Since the array contains all distinct elements except one and all elements lie in range 1 to n-1, we can check for a duplicate element by marking array elements as negative using the array index as a key. For each array element nums[i], invert the sign of the element present at index nums[i]. Finally, traverse the array once again, and if a positive number is found at index i, then the duplicate element is i.

The above approach takes two traversals of the array. We can achieve the same in only a single traversal. For each array element nums[i], invert the sign of the element present at index nums[i] if it is positive; otherwise, if the element is already negative, then it is a duplicate.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The duplicate element is 2

Java


Download  Run Code

Output:

The duplicate element is 2

Python


Download  Run Code

Output:

The duplicate element is 2

The time complexity of the above solution is O(n).

Approach 3: Using XOR

We can also solve this problem by taking xor of all array elements with numbers 1 to n-1. Since the same elements will cancel each other as a^a = 0, 0^0 = 0 and a^0 = a, we will be left with the duplicate element. This approach is demonstrated below in C, Java, and Python:

C


Download  Run Code

Output:

The duplicate element is 4

Java


Download  Run Code

Output:

The duplicate element is 4

Python


Download  Run Code

Output:

The duplicate element is 4

The time complexity of the above solution is O(n).

Approach 4: Using Difference in Sum

Finally, the post is incomplete without this textbook solution: find the sum of all element and find the difference between it and all elements which are supposed to be there. This is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

The duplicate element is 4

Java


Download  Run Code

Output:

The duplicate element is 4

Python


Download  Run Code

Output:

The duplicate element is 4

The time complexity of the above solution is O(n).