You are given an array of n+2 elements. All elements of the array are in range 1 to n. And all elements occur once except two numbers which occur twice. Find the two repeating numbers.
Example:
Input:
arr = [4, 2, 4, 5, 2, 3, 1]
n = 5
Output:
4 2
Explanation:
The above array has n + 2 = 7 elements with all elements occurring once except 2 and 4 which occur twice. So the output should be 4 2.
Method 1 (Basic)
Use two loops. In the outer loop, pick elements one by one and count the number of occurrences of the picked element in the inner loop.
This method doesn’t use the other useful data provided in questions like range of numbers is between 1 to n and there are only two repeating elements.

C++
#include<bits/stdc++.h>
using namespace std;
void printTwoRepeatNumber( int arr[], int size)
{
int i, j, display=0;
int visited[size];
for (i = 0; i < size; i++)
{
if (visited[i] == 1)
{
continue ;
}
int count = 0;
for (j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
{
visited[j] = 1;
++count;
break ;
}
}
if ( (count > 0) && (display < 2)){
++display;
cout<< "repeating element = " << arr[i]<<endl;
}
}
}
int main()
{
int arr[] = {4, 2, 5, 2, 3, 3, 4, 4, 1, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printTwoRepeatNumber(arr, arr_size);
}
|
C
#include<stdio.h>
#include<stdlib.h>
void printRepeating( int arr[], int size)
{
int i, j;
printf ( " Repeating elements are " );
for (i = 0; i < size-1; i++)
for (j = i+1; j < size; j++)
if (arr[i] == arr[j])
printf ( " %d " , arr[i]);
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
getchar ();
return 0;
}
|
Java
class RepeatElement
{
void printRepeating( int arr[], int size)
{
int i, j;
System.out.println( "Repeated Elements are :" );
for (i = 0 ; i < size- 1 ; i++)
{
for (j = i + 1 ; j < size; j++)
{
if (arr[i] == arr[j])
System.out.print(arr[i] + " " );
}
}
}
public static void main(String[] args)
{
RepeatElement repeat = new RepeatElement();
int arr[] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 };
int arr_size = arr.length;
repeat.printRepeating(arr, arr_size);
}
}
|
Python3
def printRepeating(arr, size):
print ( "Repeating elements are " ,
end = '')
for i in range ( 0 , size - 1 ):
for j in range (i + 1 , size):
if arr[i] = = arr[j]:
print (arr[i], end = ' ' )
arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ]
arr_size = len (arr)
printRepeating(arr, arr_size)
|
C#
using System;
class GFG
{
static void printRepeating( int []arr, int size)
{
int i, j;
Console.Write( "Repeated Elements are :" );
for (i = 0; i < size-1; i++)
{
for (j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
Console.Write(arr[i] + " " );
}
}
}
public static void Main()
{
int []arr = {4, 2, 4, 5, 2, 3, 1};
int arr_size = arr.Length;
printRepeating(arr, arr_size);
}
}
|
PHP
<?php
function printRepeating( $arr , $size )
{
$i ;
$j ;
echo " Repeating elements are " ;
for ( $i = 0; $i < $size -1; $i ++)
for ( $j = $i + 1; $j < $size ; $j ++)
if ( $arr [ $i ] == $arr [ $j ])
echo $arr [ $i ], " " ;
}
$arr = array (4, 2, 4, 5, 2, 3, 1);
$arr_size = sizeof( $arr , 0);
printRepeating( $arr , $arr_size );
?>
|
Javascript
<script>
function printRepeating(arr , size)
{
var i, j;
document.write( "Repeated Elements are :" );
for (i = 0; i < size-1; i++)
{
for (j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
document.write(arr[i] + " " );
}
}
}
var arr = [4, 2, 4, 5, 2, 3, 1];
var arr_size = arr.length;
printRepeating(arr, arr_size);
</script>
|
Output
Repeating elements are 4 2
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Method 2 (Use Count array)
Traverse the array once. While traversing, keep track of count of all elements in the array using a temp array count[] of size n, when you see an element whose count is already set, print it as duplicate.
This method uses the range given in the question to restrict the size of count[], but doesn’t use the data that there are only two repeating elements.
C++
#include <bits/stdc++.h>
using namespace std;
void printRepeating( int arr[], int size)
{
int *count = new int [ sizeof ( int )*(size - 2)];
int i;
cout << " Repeating elements are " ;
for (i = 0; i < size; i++)
{
if (count[arr[i]] == 1)
cout << arr[i] << " " ;
else
count[arr[i]]++;
}
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
void printRepeating( int arr[], int size)
{
int *count = ( int *) calloc ( sizeof ( int ), (size - 2));
int i;
printf ( " Repeating elements are " );
for (i = 0; i < size; i++)
{
if (count[arr[i]] == 1)
printf ( " %d " , arr[i]);
else
count[arr[i]]++;
}
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
getchar ();
return 0;
}
|
Java
class RepeatElement
{
void printRepeating( int arr[], int size)
{
int count[] = new int [size];
int i;
System.out.println( "Repeated elements are : " );
for (i = 0 ; i < size; i++)
{
if (count[arr[i]] == 1 )
System.out.print(arr[i] + " " );
else
count[arr[i]]++;
}
}
public static void main(String[] args)
{
RepeatElement repeat = new RepeatElement();
int arr[] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 };
int arr_size = arr.length;
repeat.printRepeating(arr, arr_size);
}
}
|
Python3
def printRepeating(arr,size) :
count = [ 0 ] * size
print ( " Repeating elements are " ,end = "")
for i in range ( 0 , size) :
if (count[arr[i]] = = 1 ) :
print (arr[i], end = " " )
else :
count[arr[i]] = count[arr[i]] + 1
arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ]
arr_size = len (arr)
printRepeating(arr, arr_size)
|
C#
using System;
class GFG
{
static void printRepeating( int []arr,
int size)
{
int []count = new int [size];
int i;
Console.Write( "Repeated elements are: " );
for (i = 0; i < size; i++)
{
if (count[arr[i]] == 1)
Console.Write(arr[i] + " " );
else
count[arr[i]]++;
}
}
public static void Main()
{
int []arr = {4, 2, 4, 5, 2, 3, 1};
int arr_size = arr.Length;
printRepeating(arr, arr_size);
}
}
|
PHP
<?php
function printRepeating( $arr , $size )
{
$count = array_fill (0, $size , 0);
echo "Repeated elements are " ;
for ( $i = 0; $i < $size ; $i ++)
{
if ( $count [ $arr [ $i ]] == 1)
echo $arr [ $i ]. " " ;
else
$count [ $arr [ $i ]]++;
}
}
$arr = array (4, 2, 4, 5, 2, 3, 1);
$arr_size = count ( $arr );
printRepeating( $arr , $arr_size );
?>
|
Javascript
<script>
function printRepeating(arr, size)
{
let count = new Array(size);
count.fill(0);
let i;
document.write( "Repeating elements are " );
for (i = 0; i < size; i++)
{
if (count[arr[i]] == 1)
document.write(arr[i] + " " );
else
count[arr[i]]++;
}
}
let arr = [4, 2, 4, 5, 2, 3, 1];
let arr_size = arr.length;
printRepeating(arr, arr_size);
</script>
|
Output
Repeating elements are 4 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 (Make two equations)
Let the numbers which are being repeated are X and Y. We make two equations for X and Y and the simple task left is to solve the two equations.
We know the sum of integers from 1 to n is n(n+1)/2 and product is n!. We calculate the sum of input array when this sum is subtracted from n(n+1)/2, we get X + Y because X and Y are the two numbers missing from set [1..n]. Similarly calculate the product of input array, when this product is divided from n!, we get X*Y. Given the sum and product of X and Y, we can find easily out X and Y.
Let summation of all numbers in the array be S and product be P
X + Y = S – n(n+1)/2
XY = P/n!
Using the above two equations, we can find out X and Y. For array = 4 2 4 5 2 3 1, we get S = 21 and P as 960.
X + Y = 21 – 15 = 6
XY = 960/5! = 8
X – Y = sqrt((X+Y)^2 – 4*XY) = sqrt(4) = 2
Using below two equations, we easily get X = (6 + 2)/2 and Y = (6-2)/2
X + Y = 6
X – Y = 2
Thanks to geek4u for suggesting this method. As pointed by Beginner, there can be an addition and multiplication overflow problem with this approach.
The methods 3 and 4 use all useful information given in the question :
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n);
void printRepeating( int arr[], int size)
{
int S = 0;
int P = 1;
int x, y;
int D;
int n = size - 2, i;
for (i = 0; i < size; i++)
{
S = S + arr[i];
P = P*arr[i];
}
S = S - n*(n+1)/2;
P = P/fact(n);
D = sqrt (S*S - 4*P);
x = (D + S)/2;
y = (S - D)/2;
cout<< "The two Repeating elements are " <<x<< " & " <<y;
}
int fact( int n)
{
return (n == 0)? 1 : n*fact(n-1);
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int fact( int n);
void printRepeating( int arr[], int size)
{
int S = 0;
int P = 1;
int x, y;
int D;
int n = size - 2, i;
for (i = 0; i < size; i++)
{
S = S + arr[i];
P = P*arr[i];
}
S = S - n*(n+1)/2;
P = P/fact(n);
D = sqrt (S*S - 4*P);
x = (D + S)/2;
y = (S - D)/2;
printf ( "The two Repeating elements are %d & %d" , x, y);
}
int fact( int n)
{
return (n == 0)? 1 : n*fact(n-1);
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
getchar ();
return 0;
}
|
Java
class RepeatElement
{
void printRepeating( int arr[], int size)
{
int S = 0 ;
int P = 1 ;
int x, y;
int D;
int n = size - 2 , i;
for (i = 0 ; i < size; i++)
{
S = S + arr[i];
P = P * arr[i];
}
S = S - n * (n + 1 ) / 2 ;
P = P / fact(n);
D = ( int ) Math.sqrt(S * S - 4 * P);
x = (D + S) / 2 ;
y = (S - D) / 2 ;
System.out.println( "The two repeating elements are :" );
System.out.print(x + " " + y);
}
int fact( int n)
{
return (n == 0 ) ? 1 : n * fact(n - 1 );
}
public static void main(String[] args) {
RepeatElement repeat = new RepeatElement();
int arr[] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 };
int arr_size = arr.length;
repeat.printRepeating(arr, arr_size);
}
}
|
Python3
import math
def printRepeating(arr, size) :
S = 0 ;
P = 1 ;
n = size - 2
for i in range ( 0 , size) :
S = S + arr[i]
P = P * arr[i]
S = S - n * (n + 1 ) / / 2
P = P / / fact(n)
D = math.sqrt(S * S - 4 * P)
x = (D + S) / / 2
y = (S - D) / / 2
print ( "The two Repeating elements are " ,
( int )(x), " & " ,( int )(y))
def fact(n) :
if (n = = 0 ) :
return 1
else :
return (n * fact(n - 1 ))
arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ]
arr_size = len (arr)
printRepeating(arr, arr_size)
|
C#
using System;
class GFG
{
static void printRepeating( int []arr, int size)
{
int S = 0;
int P = 1;
int x, y;
int D;
int n = size - 2, i;
for (i = 0; i < size; i++)
{
S = S + arr[i];
P = P * arr[i];
}
S = S - n * (n + 1) / 2;
P = P / fact(n);
D = ( int ) Math.Sqrt(S * S - 4 * P);
x = (D + S) / 2;
y = (S - D) / 2;
Console.WriteLine( "The two" +
" repeating elements are :" );
Console.Write(x + " " + y);
}
static int fact( int n)
{
return (n == 0) ? 1 : n * fact(n - 1);
}
public static void Main() {
int []arr = {4, 2, 4, 5, 2, 3, 1};
int arr_size = arr.Length;
printRepeating(arr, arr_size);
}
}
|
PHP
<?php
function fact( $n ){
return ( $n == 0)? 1 : $n *fact( $n -1);
}
function printRepeating( $arr , $size )
{
$S = 0;
$P = 1;
$x ; $y ;
$D ;
$n = $size - 2;
for ( $i = 0; $i < $size ; $i ++)
{
$S = $S + $arr [ $i ];
$P = $P * $arr [ $i ];
}
$S = $S - $n *( $n +1)/2;
$P = $P /fact( $n );
$D = sqrt( $S * $S - 4* $P );
$x = ( $D + $S )/2;
$y = ( $S - $D )/2;
echo "The two Repeating elements are " . $x . " & " . $y ;
}
$arr = array (4, 2, 4, 5, 2, 3, 1);
$arr_size = count ( $arr );
printRepeating( $arr , $arr_size );
?>
|
Javascript
<script>
function printRepeating(arr , size)
{
var S = 0;
var P = 1;
var x, y;
var D;
var n = size - 2, i;
for (i = 0; i < size; i++)
{
S = S + arr[i];
P = P * arr[i];
}
S = S - n * parseInt((n + 1) / 2);
P = parseInt(P / fact(n));
D = parseInt( Math.sqrt(S * S - 4 * P));
x = parseInt((D + S) / 2);
y = parseInt((S - D) / 2);
document.write( "The two repeating elements are : " );
document.write(x + " & " + y);
}
function fact(n)
{ var ans = false ;
if (n == 0)
return 1;
else
return (n * fact(n - 1));
}
var arr = [4, 2, 4, 5, 2, 3, 1];
var arr_size = arr.length;
printRepeating(arr, arr_size);
</script>
|
Output
The two Repeating elements are 4 & 2
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 4 (Use XOR)
Thanks to neophyte for suggesting this method.
The approach used here is similar to method 2 of this post.
Let the repeating numbers be X and Y, if we xor all the elements in the array and all integers from 1 to n, then the result is X xor Y.
The 1’s in binary representation of X xor Y is corresponding to the different bits between X and Y. Suppose that the kth bit of X xor Y is 1, we can xor all the elements in the array and all integers from 1 to n, whose kth bits are 1. The result will be one of X and Y.
C++
#include <bits/stdc++.h>
using namespace std;
void printRepeating( int arr[], int size)
{
int Xor = arr[0];
int set_bit_no;
int i;
int n = size - 2;
int x = 0, y = 0;
for (i = 1; i < size; i++)
Xor ^= arr[i];
for (i = 1; i <= n; i++)
Xor ^= i;
set_bit_no = Xor & ~(Xor-1);
for (i = 0; i < size; i++)
{
if (arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for (i = 1; i <= n; i++)
{
if (i & set_bit_no)
x = x ^ i;
else
y = y ^ i;
}
cout<< "The two repeating elements are " <<y<< " " <<x;
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
return 0;
}
|
C
void printRepeating( int arr[], int size)
{
int xor = arr[0];
int set_bit_no;
int i;
int n = size - 2;
int x = 0, y = 0;
for (i = 1; i < size; i++)
xor ^= arr[i];
for (i = 1; i <= n; i++)
xor ^= i;
set_bit_no = xor & ~(xor-1);
for (i = 0; i < size; i++)
{
if (arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for (i = 1; i <= n; i++)
{
if (i & set_bit_no)
x = x ^ i;
else
y = y ^ i;
}
printf ( "n The two repeating elements are %d & %d " , x, y);
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
getchar ();
return 0;
}
|
Java
class RepeatElement
{
void printRepeating( int arr[], int size)
{
int xor = arr[ 0 ];
int set_bit_no;
int i;
int n = size - 2 ;
int x = 0 , y = 0 ;
for (i = 1 ; i < size; i++)
xor ^= arr[i];
for (i = 1 ; i <= n; i++)
xor ^= i;
set_bit_no = (xor & ~(xor - 1 ));
for (i = 0 ; i < size; i++) {
int a = arr[i] & set_bit_no;
if (a != 0 )
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for (i = 1 ; i <= n; i++)
{
int a = i & set_bit_no;
if (a != 0 )
x = x ^ i;
else
y = y ^ i;
}
System.out.println( "The two reppeated elements are :" );
System.out.println(x + " " + y);
}
public static void main(String[] args)
{
RepeatElement repeat = new RepeatElement();
int arr[] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 };
int arr_size = arr.length;
repeat.printRepeating(arr, arr_size);
}
}
|
Python3
def printRepeating(arr, size):
xor = arr[ 0 ]
n = size - 2
x = 0
y = 0
for i in range ( 1 , size):
xor ^ = arr[i]
for i in range ( 1 , n + 1 ):
xor ^ = i
set_bit_no = xor & ~(xor - 1 )
for i in range ( 0 , size):
if (arr[i] & set_bit_no):
x = x ^ arr[i]
else :
y = y ^ arr[i]
for i in range ( 1 , n + 1 ):
if (i & set_bit_no):
x = x ^ i
else :
y = y ^ i
print ( "The two repeating" ,
"elements are" , y, x)
arr = [ 4 , 2 , 4 ,
5 , 2 , 3 , 1 ]
arr_size = len (arr)
printRepeating(arr, arr_size)
|
C#
using System;
class GFG
{
static void printRepeating( int []arr, int size)
{
int xor = arr[0];
int set_bit_no;
int i;
int n = size - 2;
int x = 0, y = 0;
for (i = 1; i < size; i++)
xor ^= arr[i];
for (i = 1; i <= n; i++)
xor ^= i;
set_bit_no = (xor & ~(xor - 1));
for (i = 0; i < size; i++) {
int a = arr[i] & set_bit_no;
if (a != 0)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for (i = 1; i <= n; i++)
{
int a = i & set_bit_no;
if (a != 0)
x = x ^ i;
else
y = y ^ i;
}
Console.WriteLine( "The two" +
" reppeated elements are :" );
Console.Write(x + " " + y);
}
public static void Main()
{
int []arr = {4, 2, 4, 5, 2, 3, 1};
int arr_size = arr.Length;
printRepeating(arr, arr_size);
}
}
|
PHP
<?php
function printRepeating( $arr , $size )
{
$xor = $arr [0];
$set_bit_no ;
$i ;
$n = $size - 2;
$x = 0; $y = 0;
for ( $i = 1; $i < $size ; $i ++)
$xor ^= $arr [ $i ];
for ( $i = 1; $i <= $n ; $i ++)
$xor ^= $i ;
$set_bit_no = $xor & ~( $xor -1);
for ( $i = 0; $i < $size ; $i ++)
{
if ( $arr [ $i ] & $set_bit_no )
$x = $x ^ $arr [ $i ];
else
$y = $y ^ $arr [ $i ];
}
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $i & $set_bit_no )
$x = $x ^ $i ;
else
$y = $y ^ $i ;
}
echo "n The two repeating elements are " ;
echo $y . " " . $x ;
}
$arr = array (4, 2, 4, 5, 2, 3, 1);
$arr_size = count ( $arr );
printRepeating( $arr , $arr_size );
?>
|
Javascript
<script>
function printRepeating( arr, size)
{
let Xor = arr[0];
let set_bit_no;
let i;
let n = size - 2;
let x = 0, y = 0;
for (i = 1; i < size; i++)
Xor ^= arr[i];
for (i = 1; i <= n; i++)
Xor ^= i;
set_bit_no = Xor & ~(Xor-1);
for (i = 0; i < size; i++)
{
if (arr[i] & set_bit_no)
x = x ^ arr[i];
else
y = y ^ arr[i];
}
for (i = 1; i <= n; i++)
{
if (i & set_bit_no)
x = x ^ i;
else
y = y ^ i;
}
document.write( "The two repeating elements are " +y+ " " +x);
}
let arr = [4, 2, 4, 5, 2, 3, 1];
let arr_size = arr.length;
printRepeating(arr, arr_size);
</script>
|
Output
The two repeating elements are 4 2
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 5 (Use array elements as index)
Thanks to Manish K. Aasawat for suggesting this method.
Traverse the array. Do following for every index i of A[].
{
check for sign of A[abs(A[i])] ;
if positive then
make it negative by A[abs(A[i])]=-A[abs(A[i])];
else // i.e., A[abs(A[i])] is negative
this element (ith element of list) is a repetition
}
Example: A[] = {1, 1, 2, 3, 2}
i=0;
Check sign of A[abs(A[0])] which is A[1]. A[1] is positive, so make it negative.
Array now becomes {1, -1, 2, 3, 2}
i=1;
Check sign of A[abs(A[1])] which is A[1]. A[1] is negative, so A[1] is a repetition.
i=2;
Check sign of A[abs(A[2])] which is A[2]. A[2] is positive, so make it negative. '
Array now becomes {1, -1, -2, 3, 2}
i=3;
Check sign of A[abs(A[3])] which is A[3]. A[3] is positive, so make it negative.
Array now becomes {1, -1, -2, -3, 2}
i=4;
Check sign of A[abs(A[4])] which is A[2]. A[2] is negative, so A[4] is a repetition.
Note that this method modifies the original array and may not be a recommended method if we are not allowed to modify the array.
C++
#include <bits/stdc++.h>
using namespace std;
void printRepeating( int arr[], int size)
{
int i;
cout << "The repeating elements are" ;
for (i = 0; i < size; i++)
{
if (arr[ abs (arr[i])] > 0)
arr[ abs (arr[i])] = -arr[ abs (arr[i])];
else
cout<< " " << abs (arr[i]) << " " ;
}
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
void printRepeating( int arr[], int size)
{
int i;
printf ( "\n The repeating elements are" );
for (i = 0; i < size; i++)
{
if (arr[ abs (arr[i])] > 0)
arr[ abs (arr[i])] = -arr[ abs (arr[i])];
else
printf ( " %d " , abs (arr[i]));
}
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
getchar ();
return 0;
}
|
Java
class RepeatElement
{
void printRepeating( int arr[], int size)
{
int i;
System.out.println( "The repeating elements are : " );
for (i = 0 ; i < size; i++)
{
int abs_val = Math.abs(arr[i]);
if (arr[abs_val] > 0 )
arr[abs_val] = -arr[abs_val];
else
System.out.print(abs_val + " " );
}
}
public static void main(String[] args)
{
RepeatElement repeat = new RepeatElement();
int arr[] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 };
int arr_size = arr.length;
repeat.printRepeating(arr, arr_size);
}
}
|
Python3
def printRepeating(arr, size) :
print ( " The repeating elements are" ,end = " " )
for i in range ( 0 ,size) :
if (arr[ abs (arr[i])] > 0 ) :
arr[ abs (arr[i])] = ( - 1 ) * arr[ abs (arr[i])]
else :
print ( abs (arr[i]),end = " " )
arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ]
arr_size = len (arr)
printRepeating(arr, arr_size)
|
C#
using System;
class GFG
{
static void printRepeating( int []arr, int size)
{
int i;
Console.Write( "The repeating elements are : " );
for (i = 0; i < size; i++)
{
int abs_val = Math.Abs(arr[i]);
if (arr[abs_val] > 0)
arr[abs_val] = -arr[abs_val];
else
Console.Write(abs_val + " " );
}
}
public static void Main()
{
int []arr = {4, 2, 4, 5, 2, 3, 1};
int arr_size = arr.Length;
printRepeating(arr, arr_size);
}
}
|
PHP
<?php
function printRepeating( $arr , $size )
{
$i ;
echo "The repeating elements are" , " " ;
for ( $i = 0; $i < $size ; $i ++)
{
if ( $arr [ abs ( $arr [ $i ])] > 0)
$arr [ abs ( $arr [ $i ])] = - $arr [ abs ( $arr [ $i ])];
else
echo abs ( $arr [ $i ]), " " ;
}
}
$arr = array (4, 2, 4, 5, 2, 3, 1);
$arr_size = sizeof( $arr );
printRepeating( $arr , $arr_size );
#This code is contributed by aj_36
?>
|
Javascript
<script>
function printRepeating(arr , size)
{
var i;
document.write( "The repeating elements are : " );
for (i = 0; i < size; i++)
{
var abs_val = Math.abs(arr[i]);
if (arr[abs_val] > 0)x
arr[abs_val] = -arr[abs_val];
else
document.write(abs_val + " " );
}
}
var arr = [4, 2, 4, 5, 2, 3, 1];
var arr_size = arr.length;
printRepeating(arr, arr_size);
</script>
|
Output
The repeating elements are 4 2
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 6 (Similar to method 5)
Thanks to Vivek Kumar for suggesting this method.
The point is to increment every element at (arr[i]th-1)th index by N-1 (as the elements are present upto N-2 only) and at the same time check if element at that index when divided by (N-1) gives 2. If this is true then it means that the element has appeared twice and we can easily say that this is one of our answers. This is one of the very useful techniques when we want to calculate Frequencies of Limited Range Array Elements (see this post to know more).
This method works on the fact that the remainder of an element when divided by any number greater than that element will always give that same element. Similarly, as we are incrementing each element at (arr[i]th-1) index by N-1, so when this incremented element is divided by N-1 it will give number of times we have added N-1 to it, hence giving us the occurrence of that index (according to 1-based indexing).
Below given code applies this method:
C++
#include <iostream>
using namespace std;
void twoRepeated( int arr[], int N)
{
int m = N - 1;
for ( int i = 0; i < N; i++) {
arr[arr[i] % m - 1] += m;
if ((arr[arr[i] % m - 1] / m) == 2)
cout << arr[i] % m << " " ;
}
}
int main()
{
int arr[] = { 4, 2, 4, 5, 2, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "The two repeating elements are " ;
twoRepeated(arr, n);
return 0;
}
|
C
#include <stdio.h>
void twoRepeated( int arr[], int N)
{
int m = N - 1;
for ( int i = 0; i < N; i++) {
arr[arr[i] % m - 1] += m;
if ((arr[arr[i] % m - 1] / m) == 2)
printf ( "%d " , arr[i]);
}
}
int main()
{
int arr[] = { 4, 2, 4, 5, 2, 3, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "The two repeating elements are " );
twoRepeated(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void twoRepeated( int arr[], int N)
{
int m = N - 1 ;
for ( int i = 0 ; i < N; i++) {
arr[arr[i] % m - 1 ] += m;
if ((arr[arr[i] % m - 1 ] / m) == 2 )
System.out.printf(arr[i] % m + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 };
int n = 7 ;
System.out.printf(
"The two repeating elements are " );
twoRepeated(arr, n);
}
}
|
Python3
def twoRepeated(arr, N):
m = N - 1
for i in range (N):
arr[arr[i] % m - 1 ] + = m
if ((arr[arr[i] % m - 1 ] / / m) = = 2 ):
print (arr[i] % m ,end = " " )
arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ]
n = len (arr)
print ( "The two repeating elements are " , end = "")
twoRepeated(arr, n)
|
C#
using System;
public class GFG
{
public static void twoRepeated( int [] arr, int N)
{
int m = N - 1;
for ( int i = 0; i < N; i++)
{
arr[arr[i] % m - 1] += m;
if ((arr[arr[i] % m - 1] / m) == 2)
Console.Write(arr[i] % m + " " );
}
}
public static void Main(String []args)
{
int [] arr = { 4, 2, 4, 5, 2, 3, 1 };
int n = 7;
Console.Write( "The two repeating elements are " );
twoRepeated(arr, n);
}
}
|
Javascript
<script>
function twoRepeated(arr, N)
{
let m = N - 1;
for (let i = 0; i < N; i++)
{
arr[parseInt(arr[i] % m) - 1] += m;
if (parseInt(arr[parseInt(arr[i] % m) - 1] / m) == 2)
document.write(parseInt(arr[i] % m) + " " );
}
}
var arr = [ 4, 2, 4, 5, 2, 3, 1 ];
var n = 7;
document.write( "The two repeating elements are " );
twoRepeated(arr, n);
</script>
|
Output
The two repeating elements are 4 2
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 7
The point here is to enter the array elements one by one into the unordered set. If a particular element is already present in the set it’s a repeating element.
C++
#include <bits/stdc++.h>
using namespace std;
void printRepeating( int arr[], int size)
{
unordered_set< int >s;
cout<< "The two Repeating elements are : " ;
for ( int i=0;i<size;i++)
{
if (s.empty()== false && s.find(arr[i])!=s.end())
cout<<arr[i]<< " " ;
s.insert(arr[i]);
}
}
int main()
{
int arr[] = {4, 2, 4, 5, 2, 3, 1};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
printRepeating(arr, arr_size);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printRepeating( int arr[], int size)
{
HashSet<Integer>s = new HashSet<>();
System.out.print( "The two Repeating elements are : " );
for ( int i = 0 ; i < size; i++)
{
if (!s.isEmpty() && s.contains(arr[i]))
System.out.print(arr[i]+ " " );
s.add(arr[i]);
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 };
int arr_size = arr.length;
printRepeating(arr, arr_size);
}
}
|
Python3
def printRepeating(arr, size):
s = set ()
print ( "The two Repeating elements are : " , end = "")
for i in range (size):
if ( len (s) and arr[i] in s):
print (arr[i], end = " " )
s.add(arr[i])
arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ]
arr_size = len (arr)
printRepeating(arr, arr_size)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void printRepeating( int [] arr, int size)
{
HashSet< int >s = new HashSet< int >();
Console.Write( "The two Repeating elements are : " );
for ( int i = 0; i < size; i++)
{
if (s.Count != 0 && s.Contains(arr[i]))
Console.Write(arr[i] + " " );
s.Add(arr[i]);
}
}
public static void Main()
{
int [] arr = {4, 2, 4, 5, 2, 3, 1};
int arr_size = arr.Length;
printRepeating(arr, arr_size);
}
}
|
Javascript
<script>
function printRepeating(arr, size)
{
const s = new Set();
document.write( "The two repeating elements are " );
for (let i = 0; i < size; i++)
{
if (s.size != 0 && s.has(arr[i]))
document.write(arr[i] + " " );
s.add(arr[i]);
}
}
var arr = [ 4, 2, 4, 5, 2, 3, 1 ];
var arr_size = 7;
printRepeating(arr, arr_size);
</script>
|
Output
The two Repeating elements are : 4 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem.