Given a sorted array of n elements containing elements in range from 1 to n-1 i.e. one element occurs twice, the task is to find the repeating element in an array.
Examples :
Input : arr[] = { 1, 2 , 3 , 4 , 4}
Output : 4
Input : arr[] = { 1 , 1 , 2 , 3 , 4}
Output : 1
Brute Force:
- Traverse the input array using a for a loop.
- For each element in the array, traverse the remaining part of the array using another for loop.
- For each subsequent element, check if it is equal to the current element.
- If a match is found, return the current element.
- If no match is found, continue with the next element in the outer loop.
- If the outer loop completes without finding a match, return -1 to indicate that there is no repeating element in the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int FindRepeatingElement( int arr[], int size){
for ( int i=0; i<size; i++){
for ( int j=i+1; j<size; j++){
if (arr[i] == arr[j])
return i;
}
}
return -1;
}
int main()
{
int arr[] = {1, 2 , 3 , 4 , 4};
int n = sizeof (arr) / sizeof (arr[0]);
int index = FindRepeatingElement(arr, n);
if (index != -1)
cout << arr[index];
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int findRepeatingElement( int arr[], int size){
for ( int i= 0 ; i<size; i++){
for ( int j=i+ 1 ; j<size; j++){
if (arr[i] == arr[j])
return i;
}
}
return - 1 ;
}
public static void main(String[] args) {
int arr[] = { 1 , 2 , 3 , 4 , 4 };
int n = arr.length;
int index = findRepeatingElement(arr, n);
if (index != - 1 )
System.out.println(arr[index]);
}
}
|
Python3
def FindRepeatingElement(arr, size):
for i in range (size):
for j in range (i + 1 , size):
if arr[i] = = arr[j]:
return arr[i]
return - 1
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 , 4 ]
n = len (arr)
element = FindRepeatingElement(arr, n)
if element ! = - 1 :
print (element)
|
C#
using System;
public class Program
{
public static int FindRepeatingElement( int [] arr, int size)
{
for ( int i = 0; i < size; i++)
{
for ( int j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
{
return i;
}
}
}
return -1;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 4 };
int n = arr.Length;
int index = FindRepeatingElement(arr, n);
if (index != -1)
{
Console.WriteLine(arr[index]);
}
}
}
|
Javascript
function FindRepeatingElement(arr, size)
{
for (let i=0; i<size; i++)
{
for (let j=i+1; j<size; j++)
{
if (arr[i] == arr[j])
return i;
}
}
return -1;
}
let arr = [1, 2 , 3 , 4 , 4];
let n = arr.length;
let index = FindRepeatingElement(arr, n);
if (index != -1)
console.log(arr[index]);
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
An efficient method is to use Binary Search.
Observation: If an element ‘X’ is repeating, then it must be at index ‘X’ in the array. So the problem reduces to find any element whose value is same as its index.
C++
#include <bits/stdc++.h>
using namespace std;
int FindRepeatingElement( int arr[], int size){
int lo = 0;
int hi = size - 1;
int mid;
while (lo <= hi){
mid = (lo+hi)/2;
if (arr[mid] <= mid){
hi = mid-1;
}
else {
lo = mid + 1;
}
}
return lo;
}
int main()
{
int arr[] = {1, 2, 3, 3, 4, 5};
int n = sizeof (arr) / sizeof (arr[0]);
int index = FindRepeatingElement(arr, n);
if (index != -1)
cout << arr[index];
return 0;
}
|
Java
class Test
{
static int findRepeatingElement( int arr[], int low, int high)
{
if (low > high)
return - 1 ;
int mid = (low + high) / 2 ;
if (arr[mid] != mid + 1 )
{
if (mid > 0 && arr[mid]==arr[mid- 1 ])
return mid;
return findRepeatingElement(arr, low, mid- 1 );
}
return findRepeatingElement(arr, mid+ 1 , high);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 3 , 4 , 5 };
int index = findRepeatingElement(arr, 0 , arr.length- 1 );
if (index != - 1 )
System.out.println(arr[index]);
}
}
|
Python3
def findRepeatingElement(arr, low, high):
if low > high:
return - 1
mid = (low + high) / / 2
if (arr[mid] ! = mid + 1 ):
if (mid > 0 and arr[mid] = = arr[mid - 1 ]):
return mid
return findRepeatingElement(arr, low, mid - 1 )
return findRepeatingElement(arr, mid + 1 , high)
arr = [ 1 , 2 , 3 , 3 , 4 , 5 ]
n = len (arr)
index = findRepeatingElement(arr, 0 , n - 1 )
if (index is not - 1 ):
print (arr[index])
|
Javascript
<script>
function findRepeatingElement(arr, low, high)
{
if (low > high) return -1;
var mid = parseInt((low + high) / 2);
if (arr[mid] != mid + 1)
{
if (mid > 0 && arr[mid] == arr[mid - 1]) return mid;
return findRepeatingElement(arr, low, mid - 1);
}
return findRepeatingElement(arr, mid + 1, high);
}
var arr = [1, 2, 3, 3, 4, 5];
var n = arr.length;
var index = findRepeatingElement(arr, 0, n - 1);
if (index != -1) document.write(arr[index]);
</script>
|
C#
using System;
class Test
{
static int findRepeatingElement( int []arr, int low,
int high)
{
if (low > high)
return -1;
int mid = (low + high) / 2;
if (arr[mid] != mid + 1)
{
if (mid > 0 && arr[mid]==arr[mid-1])
return mid;
return findRepeatingElement(arr, low, mid-1);
}
return findRepeatingElement(arr, mid+1, high);
}
public static void Main()
{
int []arr = {1, 2, 3, 3, 4, 5};
int index = findRepeatingElement(arr, 0, arr.Length-1);
if (index != -1)
Console.Write(arr[index]);
}
}
|
PHP
<?php
function findRepeatingElement( $arr ,
$low ,
$high )
{
if ( $low > $high )
return -1;
$mid = floor (( $low + $high ) / 2);
if ( $arr [ $mid ] != $mid + 1)
{
if ( $mid > 0 && $arr [ $mid ] ==
$arr [ $mid - 1])
return $mid ;
return findRepeatingElement( $arr , $low ,
$mid - 1);
}
return findRepeatingElement( $arr , $mid + 1,
$high );
}
$arr = array (1, 2, 3, 3, 4, 5);
$n = sizeof( $arr );
$index = findRepeatingElement( $arr , 0,
$n - 1);
if ( $index != -1)
echo $arr [ $index ];
?>
|
Time Complexity : O(log n)
Space Complexity: O(1)
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Sep, 2023
Like Article
Save Article