Last Updated: 27 Jan, 2021

Find Duplicate in Array

Easy
Asked in companies
OlaAmazonBNY Mellon

Problem statement

You are given an array of integers 'ARR' containing N elements. Each integer is in the range [1, N-1], with exactly one element repeated in the array.

Your task is to find the duplicate element. The duplicate element may be repeated more than twice in the error, but there will be exactly one element that is repeated in the array.

Note :

All the integers in the array appear only once except for precisely one integer which appears two or more times.

Input format:

The first line of input contains an integer ‘T’ denoting the number of test cases. Then the T test cases follow.

The first line of each test case contains an integer ‘N’, the number of elements in the array. 

The second line of each test case contains ‘N’ space-separated integers representing the elements of the array. 

Output format:

For each test case, the duplicate element of the given array is printed.

The output of each test case is printed in a separate line. 
Note :
You are not supposed to print anything; It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 5
1 <= N <= 10^5
1 <= ARR[i] <= N - 1

Time Limit: 1 sec

Approaches

01 Approach

First, we sort the array. Now, the rest of the algorithm becomes fairly simple. We just compare each element to its previous element. As we know there is exactly one repeated element, hence we can simply just return the element, which is equal to its previous element as soon as we find it.

02 Approach

We perform binary search on the array [1,N], then COUNT the number of elements that are less than or equal to MID of [1,N]. If the duplicate is on the left side of the middle element, then the COUNT should be more than MID, else it is on the right side. Narrow down the search space each time until you find the repeating number then return it.

Example:     Input: 2 4 1 5 3 6 8 7 4

 

Explanation: 

LOW = 1, HIGH = 9, MID = 5

COUNT elements smaller or equal to MID: count = 6

COUNT > MID , hence the repeated element is less than 5 -> 

HIGH = MID = 5, hence new MID = 3, now count elements smaller or equal to mid: COUNT = 3

COUNT <= MID, hence the repeated element is greater than 3 ->

LOW = MID + 1 = 4, hence new MID = 4, now count elements smaller or equal to mid: COUNT = 5   

COUNT > MID , hence the repeated element is less than or equal to 4 -> 

HIGH = MID = 4, hence new MID = old MID = 4, hence repeated element = 4.

Output: 

4

03 Approach

We need to insert elements into a data structure and look them up in constant time, so as to improve the complexity of sorting approach. A set can be used to satisfy these conditions. So, we iterate over the array and insert each element into a set. Before inserting we check whether the element is already present in the set or not. If it is found, we get the repeated element and hence return it.

04 Approach

For this approach we use the array indices to store the visited state of each number. We know that only the duplicate element would be visited more than once. For each number we go to its index position and multiply it with ’-1’, thus making it negative. In case of duplicate, it will visit twice and hence will become positive, which will be returned.

Example:     Input: 2 4 1 5 3 6 8 7 4

 

Explanation: 

Traverse from start and multiply each element with ‘-1’ and checking if after multiplication it becomes positive then return it.

2 4 1 5 3 6 8 7 4

-2 -4 -1 -5 -3 -6 -8 -7 4

4 is positive hence return 4

Output: 

4

05 Approach

For this approach, we divide the whole process into two phases and use two pointers named tortoise and hare.

Phase 1(Find the intersection point): The hare would be twice as fast as the tortoise. Hence, HARE = ARR[ARR[HARE]] and TORTOISE = ARR[TORTOISE]. Since the tortoise is slow, the hare would catch it at some point, this point will be our intersection point. Note that the intersection point may not be equal to the starting point of the loop. 

Example: ARR = 2 4 1 5 3 6 8 7 4

     

 

Hare: 2 1 3 8 4 5 6 7

Tortoise: 2 4 1 5 3 6 8 7

In the above array, we can see that the intersection point of hare and tortoise will be 7 but the starting point of the loop is 4.

After finding the intersection point, we start with the second phase:

Phase 2: 

Now, we reduce the speed of the hare and make it equal to the speed of the tortoise, meaning now HARE = ARR[HARE] and TORTOISE = ARR[TORTOISE]. The tortoise starts from the start of the array, while the hare starts from the intersection point found in the first phase. Now, the point where the hare and the tortoise intersect will be the starting point of the loop which is the repeated element and hence returned.