# Find duplicates under given constraints

A sorted array contains 6 different numbers, only 1 number is repeated five times. So there are total 10 numbers in array. Find the duplicate numbers using two comparisons only.

Examples :

```Input: arr[] = {1, 1, 1, 1, 1, 5, 7, 10, 20, 30}
Output: 1

Input: arr[] = {1, 2, 3, 3, 3, 3, 3, 5, 9, 10}
Output: 3
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

An important observation is, arr or arr is an occurrence of repeated element for sure. Below is the implementation based on this observation.

## CPP

 `// C++ program to find duplicate element under ` `// given constraints. ` `#include ` `using` `namespace` `std; ` ` `  `// This function assumes array is sorted,  has ` `// 10 elements,  there are totoal 6 different ` `// elements and one element repeats 5 times. ` `int` `findDuplicate(``int` `a[]) ` `{ ` `    ``if` `(a == a) ` `        ``return` `a; ` `    ``else` `if` `(a == a) ` `        ``return` `a; ` `    ``else` `        ``return` `a; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = {1, 1, 1, 1, 1, 5, 7, 10, 20, 30}; ` `    ``cout << findDuplicate(a); ` `    ``return` `0; ` `} `

## JAVA

 `// Java program to find duplicate element under ` `// given constraints. ` `class` `Num{ ` ` `  `// This function assumes array is sorted, has ` `// 10 elements, there are totoal 6 different ` `// elements and one element repeats 5 times. ` `static` `int` `findDuplicate(``int` `a[]) ` `{ ` `    ``if` `(a[``3``] == a[``4``]) ` `        ``return` `a[``3``]; ` `    ``else` `if` `(a[``4``] == a[``5``]) ` `        ``return` `a[``4``]; ` `    ``else` `        ``return` `a[``5``]; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `a[] = {``1``, ``1``, ``1``, ``1``, ``1``, ``5``, ``7``, ``10``, ``20``, ``30``}; ` `    ``System.out.println(findDuplicate(a)); ` `} ` `} ` `//This code is contributed by ` `//Smitha Dinesh Semwal `

## Python3

 `# Python 3 program to find duplicate ` `# element under given constraints. ` ` `  `# This function assumes array is  ` `# sorted, has 10 elements, there are ` `# totoal 6 different elements and one ` `# element repeats 5 times. ` `def` `findDuplicate(a): ` ` `  `    ``if` `(a[``3``] ``=``=` `a[``4``]): ` `        ``return` `a[``3``]  ` `    ``elif` `(a[``4``] ``=``=` `a[``5``]): ` `        ``return` `a[``4``]  ` `    ``else``: ` `        ``return` `a[``5``]  ` ` `  `# Driver code ` `a ``=` `[``1``, ``1``, ``1``, ``1``, ``1``, ``5``, ``7``, ` `              ``10``, ``20``, ``30``]  ` ` `  `print``(findDuplicate(a))  ` ` `  `# This code is contributed by Smitha Dinesh Semwal `

## C#

 `// C# program to find duplicate   ` `// element under given constraints. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `// This function assumes array is  ` `// sorted, has 10 elements, there ` `// are totoal 6 different elements ` `// and one element repeats 5 times. ` `static` `int` `findDuplicate(``int` `[]a) ` `{ ` `    ``if` `(a == a) ` `        ``return` `a; ` `    ``else` `if` `(a == a) ` `        ``return` `a; ` `    ``else` `        ``return` `a; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]a = {1, 1, 1, 1, 1, 5, ` `               ``7, 10, 20, 30}; ` `    ``Console.Write(findDuplicate(a)); ` `} ` `} ` ` `  `// This code is contributed by nitin mittal `

## PHP

 ` `

Output :

```1
```

Exercise: Extend the above problem for an array with n different elements, size of array is 2*(n-1) and one element repeats (n-1) times.

This article is contributed by Rakesh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : nitin mittal

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.