We can modify the binary search algorithm to get a solution. In binary search , we have a key, and we used this key to find its position by dividing the size of the array. Here we do not have a key, instead we must find it. But the duplicate element behavior can be used to bisect size of the array. How? let's get a look:
A careful study of the data, we can easily see that after inserting a duplicate element in an arbitrary position (for example, index ) in a sequential array of elements, the property of the elements will change ( a[i] == i+1 → a[i] != i+1 ) from the position of index (including index ). Now this changed property can be used to divide the size of the array. Therefore, we can find a duplicate in O(log(n)) .
For example, consider this array: {1, 2, 3, 4, 5, 6, 2, 7, 8, 9, 10}
{1, 2, 3, 4, 5, 6, 2, 7, 8, 9, 10} || from this position the the property of (a[i] == i+1) will no more satisfied.
This property can be a model for dividing the size of an array in a solution.
void binary_duplictae_finder(int a[], int low, int high) { int mid=(low+high)/2; if(high - low > 1){ if(a[mid]!=mid+1) binary_duplictae_finder(a, low, mid); else binary_duplictae_finder(a, mid, high); } if(high==low+1) printf("%d ", a[high]); }