Find duplicates under given constraints

07 Jul, 2022
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
Asked in Yahoo
An important observation is, arr[4] or arr[5] is an occurrence of repeated element for sure. Below is the implementation based on this observation. 
Implementation:
CPP
JAVA
Python3
C#
PHP
Javascript
// C++ program to find duplicate element under// given constraints.#include<bits/stdc++.h>using namespace std; // This function assumes array is sorted,  has// 10 elements,  there are total 6 different// elements and one element repeats 5 times.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 codeint main(){    int a[] = {1, 1, 1, 1, 1, 5, 7, 10, 20, 30};    cout << findDuplicate(a);    return 0;}
// Java program to find duplicate element under// given constraints.class Num{ // This function assumes array is sorted, has// 10 elements, there are total 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 codepublic 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
# Python 3 program to find duplicate# element under given constraints. # This function assumes array is# sorted, has 10 elements, there are# total 6 different elements and one# element repeats 5 times.def findDuplicate(a):     if (a[3] == a[4]):        return a[3]    else if (a[4] == a[5]):        return a[4]    else:        return a[5] # Driver codea = [1, 1, 1, 1, 1, 5, 7,              10, 20, 30] print(findDuplicate(a)) # This code is contributed by Smitha Dinesh Semwal
// C# program to find duplicate // element under given constraints.using System; class GFG { // This function assumes array is// sorted, has 10 elements, there// are total 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 codepublic 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// PHP program to find duplicate// element under given constraints. // This function assumes array is// sorted, has 10 elements, there// are total 6 different elements// and one element repeats 5 times.function findDuplicate($a){    if ($a[3] == $a[4])        return $a[3];    else if ($a[4] == $a[5])        return $a[4];    else        return $a[5];} // Driver code$a = array(1, 1, 1, 1, 1,           5, 7, 10, 20, 30);echo findDuplicate($a); // This code is contributed by nitin mittal.?>
<script> // JavaScript program to find duplicate element under// given constraints.  // This function assumes array is sorted, has// 10 elements, there are total 6 different// elements and one element repeats 5 times.function findDuplicate(a){    if (a[3] == a[4])        return a[3];    else if (a[4] == a[5])        return a[4];    else        return a[5];} // Driver code    let a = [1, 1, 1, 1, 1, 5, 7, 10, 20, 30];    document.write(findDuplicate(a));     </script>
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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
nitin mittal
_saurabh_jaiswal
as5853535
surinderdawra388
hardikkoriintern
Yahoo
Arrays
Yahoo
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Search, insert and delete in an unsorted array
Window Sliding Technique
Chocolate Distribution Problem
Find duplicates in O(n) time and O(1) extra space | Set 1
Next Greater Element
Move all negative numbers to beginning and positive to end with constant extra space
What is Data Structure: Types, Classifications and Applications
Count pairs with given sum
Find subarray with given sum | Set 1 (Nonnegative Numbers)