Given an array of n elements containing elements from 0 to n-1, with any of these numbers appearing any number of times, find these repeating numbers in O(n) and using only constant memory space.
Example:
Input: n = 7 , array = {1, 2, 3, 1, 3, 6, 6}
Output: 1, 3 and 6.
Explanation: Duplicate element in the array are 1 , 3 and 6
Input: n = 6, array = {5, 3, 1, 3, 5, 5}
Output: 3 and 5.
Explanation: Duplicate element in the array are 3 and 5
We have discussed an approach for this question in the below post:
Duplicates in an array in O(n) and by using O(1) extra space | Set-2.
But there is a problem in the above approach. It prints the repeated number more than once.
Approach: The basic idea is to use a HashMap to solve the problem. But there is a catch, the numbers in the array are from 0 to n-1, and the input array has length n. So, the input array can be used as a HashMap. While traversing the array, if an element a is encountered then increase the value of a%n‘th element by n. The frequency can be retrieved by dividing the a%n‘th element by n.
Algorithm:
- Traverse the given array from start to end.
- For every element in the array increment the arr[i]%n‘th element by n.
- Now traverse the array again and print all those indices i for which arr[i]/n is greater than 1. Which guarantees that the number n has been added to that index.
Note: This approach works because all elements are in the range from 0 to n-1 and arr[i]/n would be greater than 1 only if a value “i” has appeared more than once.
Below is the implementation of the above approach:
CPP
#include <iostream>
using namespace std;
void printRepeating( int arr[], int n)
{
for ( int i = 0; i < n; i++)
{
int index = arr[i] % n;
arr[index] += n;
}
for ( int i = 0; i < n; i++)
{
if ((arr[i] / n) >= 2)
cout << i << " " ;
}
}
int main()
{
int arr[] = { 1, 6, 3, 1, 3, 6, 6 };
int arr_size = sizeof (arr) / sizeof (arr[0]);
cout << "The repeating elements are: \n" ;
printRepeating(arr, arr_size);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void printRepeating( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
{
int index = arr[i] % n;
arr[index] += n;
}
for ( int i = 0 ; i < n; i++)
{
if ((arr[i] / n) >= 2 )
System.out.print(i + " " );
}
}
public static void main(String args[])
{
int arr[] = { 1 , 6 , 3 , 1 , 3 , 6 , 6 };
int arr_size = arr.length;
System.out.println( "The repeating elements are: " );
printRepeating(arr, arr_size);
}
}
|
Python3
def printRepeating(arr, n):
for i in range ( 0 , n):
index = arr[i] % n
arr[index] + = n
for i in range ( 0 , n):
if (arr[i] / n) > = 2 :
print (i, end = " " )
arr = [ 1 , 6 , 3 , 1 , 3 , 6 , 6 ]
arr_size = len (arr)
print ( "The repeating elements are:" )
printRepeating(arr, arr_size)
|
C#
using System;
class GFG {
static void printRepeating( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
{
int index = arr[i] % n;
arr[index] += n;
}
for ( int i = 0; i < n; i++)
{
if ((arr[i] / n) >= 2)
Console.Write(i + " " );
}
}
public static void Main()
{
int [] arr = { 1, 6, 3, 1, 3, 6, 6 };
int arr_size = arr.Length;
Console.Write( "The repeating elements are: "
+ "\n" );
printRepeating(arr, arr_size);
}
}
|
PHP
<?php
function printRepeating( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$index = $arr [ $i ] % $n ;
$arr [ $index ] += $n ;
}
for ( $i = 0; $i < $n ; $i ++)
{
if (( $arr [ $i ] / $n ) >= 2)
echo $i , " " ;
}
}
$arr = array (1, 6, 3, 1, 3, 6, 6);
$arr_size = sizeof( $arr ) /
sizeof( $arr [0]);
echo "The repeating elements are: \n" ;
printRepeating( $arr , $arr_size );
?>
|
Javascript
<script>
function printRepeating(arr,n)
{
for (let i = 0; i < n; i++)
{
let index = arr[i] % n;
arr[index] += n;
}
for (let i = 0; i < n; i++)
{
if ((arr[i] / n) >= 2)
document.write(i + " " );
}
}
let arr=[1, 6, 3, 1, 3, 6, 6];
let arr_size = arr.length;
document.write( "The repeating elements are: <br>" );
printRepeating(arr, arr_size);
</script>
|
Output
The repeating elements are:
1 3 6
Complexity Analysis:
- Time Complexity: O(n).
Only two traversals are needed. So the time complexity is O(n)
- Auxiliary Space: O(1).
As no extra space is needed, so the space complexity is constant
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 :
12 Sep, 2023
Like Article
Save Article