Duplicates in an array in O(n) and by using O(1) extra space | Set-2
Given an array of n elements which contains 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.
For example, let n be 7 and array be {1, 2, 3, 1, 3, 6, 6}, the answer should be 1, 3 and 6.
We have discussed an approach for this question in below post:
Duplicates in an array in O(n) and by using O(1) extra space | Set-2.
There is a problem in above approach. It prints the repeated number more than once. For example: {1, 6, 3, 1, 3, 6, 6} it will give output as : 3 6 6. So, in this article, another method is discussed to overcome this problem.
We strongly recommend that you click here and practice it, before moving on to the solution.
1- Traverse the given array from i= 0 to n-1 elements
Go to index arr[i]%n and increment its value by n.
3- Now traverse the array again and print all those
indexes i for which arr[i]/n is greater than 1.
This approach works because all elements are in 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.
CPP
// C++ program to print all elements that // appear more than once. #include <iostream> using namespace std; // function to find repeating elements void printRepeating( int arr[], int n) { // First check all the values that are // present in an array then go to that // values as indexes and increment by // the size of array for (int i = 0; i < n; i++) { int index = arr[i] % n; arr[index] += n; } // Now check which value exists more // than once by dividing with the size // of array for (int i = 0; i < n; i++) { if ((arr[i]/n) > 1) cout << i << " "; } } // Driver's code 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
// Java program to print all elements that // appear more than once. import java.util.*; class GFG{ // function to find repeating elements static void printRepeating(int arr[], int n) { // First check all the values that are // present in an array then go to that // values as indexes and increment by // the size of array for (int i = 0; i < n; i++) { int index = arr[i] % n; arr[index] += n; } // Now check which value exists more // than once by dividing with the size // of array for (int i = 0; i < n; i++) { if ((arr[i]/n) > 1) System.out.println(i +" "); } } // Driver's code 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
# Python3 program to # print all elements that # appear more than once. # function to find # repeating elements def printRepeating(arr, n): # First check all the # values that are # present in an array # then go to that # values as indexes # and increment by # the size of array for i in range(0, n): index = arr[i] % n arr[index] += n # Now check which value # exists more # than once by dividing # with the size # of array for i in range(0,n): if (arr[i]/n) > 1: print (i , end = " ") # Driver's code arr = [1, 6, 3, 1, 3, 6, 6] arr_size = len(arr) print ("The repeating elements are:") printRepeating( arr, arr_size) # This code is contributed # by Shreyanshi Arun. |
C#
// C# program to print all elements that // appear more than once. using System; class GFG{ // function to find repeating elements static void printRepeating(int[] arr, int n) { // First check all the values that are // present in an array then go to that // values as indexes and increment by // the size of array for (int i = 0; i < n; i++) { int index = arr[i] % n; arr[index] += n; } // Now check which value exists more // than once by dividing with the size // of array for (int i = 0; i < n; i++) { if ((arr[i]/n) > 1) Console.Write(i +" "); } } // Driver's code 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 // PHP program to print all // elements that appear more // than once. // function to find // repeating elements function printRepeating( $arr, $n) { // First check all the values // that are present in an array // then go to that values as indexes // and increment by the size of array for ($i = 0; $i < $n; $i++) { $index = $arr[$i] % $n; $arr[$index] += $n; } // Now check which value // exists more than once // by dividing with the // size of array for ($i = 0; $i < $n; $i++) { if (($arr[$i] / $n) > 1) echo $i , " "; } } // Driver's code $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); // This code is contributed by nitin mittal. ?> |
Output :
The repeating elements are: 1 3 6
Time Complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Search an element in a sorted and rotated array
- Merge an array of size n into another array of size m+n
- k largest(or smallest) elements in an array | added Min Heap method
- Program for array rotation
- Write a program to reverse an array or string
- Reversal algorithm for array rotation
- Block swap algorithm for array rotation
- Leaders in an array
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Find the smallest and second smallest elements in an array
- Maximum and minimum of an array using minimum number of comparisons
- Check for Majority Element in a sorted array
- Segregate 0s and 1s in an array
- Ceiling in a sorted array



