Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 27 Jan, 2021

Easy

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

```
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.
```

```
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.
```

```
You are not supposed to print anything; It has already been taken care of. Just implement the given function.
```

```
1 <= T <= 5
1 <= N <= 10^5
1 <= ARR[i] <= N - 1
Time Limit: 1 sec
```

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.

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.

**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.**

**4**

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.

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.

**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**

**4**

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

**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.**