Given an integer array, print all repeating elements (Elements that appear more than once) in the array. The output should contain elements according to their first occurrences.
Examples:
Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}
Output: 10 45
Input: arr[] = {1, 2, 3, 4, 2, 5}
Output: 2
Input: arr[] = {1, 1, 1, 1, 1}
Output: 1
The idea is to use Hashing to solve this in O(n) time on average. We store elements and their counts in a hash table. After storing counts, we traverse input array again and print those elements whose counts are more than once. To make sure that every output element is printed only once, we set count as 0 after printing the element.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printRepeating( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
for ( int i = 0; i < n; i++) {
if (mp[arr[i]] > 1) {
cout << arr[i] << " " ;
mp[arr[i]] = 0;
}
}
}
int main()
{
int arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 };
int n = sizeof (arr) / sizeof (arr[0]);
printRepeating(arr, n);
return 0;
}
|
Java
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.lang.*;
public class GFG {
static void printRepeating( int arr[], int n)
{
Map<Integer, Integer> map
= new LinkedHashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
try {
map.put(arr[i], map.get(arr[i]) + 1 );
}
catch (Exception e) {
map.put(arr[i], 1 );
}
}
for (Entry<Integer, Integer> e : map.entrySet()) {
if (e.getValue() > 1 ) {
System.out.print(e.getKey() + " " );
}
}
}
public static void main(String[] args) throws IOException
{
int arr[] = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 };
int n = arr.length;
printRepeating(arr, n);
}
}
|
Python3
def printRepeating(arr, n):
mp = [ 0 ] * 100
for i in range ( 0 , n):
mp[arr[i]] + = 1
for i in range ( 0 , n):
if (mp[arr[i]] > 1 ):
print (arr[i], end = " " )
mp[arr[i]] = 0
arr = [ 12 , 10 , 9 , 45 ,
2 , 10 , 10 , 45 ]
n = len (arr)
printRepeating(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printRepeating( int []arr, int n)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
for ( int i = 0 ; i < n; i++)
{
if (map.ContainsKey(arr[i]))
{
var val = map[arr[i]];
map.Remove(arr[i]);
map.Add(arr[i], val + 1);
}
else
{
map.Add(arr[i], 1);
}
}
foreach (KeyValuePair< int , int > e in map)
{
if (e.Value > 1)
{
Console.Write(e.Key + " " );
}
}
}
public static void Main(String[] args)
{
int []arr = { 12, 10, 9, 45, 2, 10, 10, 45 };
int n = arr.Length;
printRepeating(arr, n);
}
}
|
Javascript
function printRepeating(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++)
{
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1)
}
for ( var i = 0; i < n; i++) {
if (mp.get(arr[i]) > 1) {
console.log( arr[i] + " " );
mp.set(arr[i], 0);
}
}
}
var arr = [ 12, 10, 9, 45, 2, 10, 10, 45 ];
var n = arr.length;
printRepeating(arr, n);
|
complexity Analysis:
- Time Complexity: O(n) under the assumption that hash insert and search functions work in O(1) time.
- Auxiliary Space: O(n), where n represents the size of the given array.
Method #2:Using Built-in Python functions:
- Count all the frequencies of all elements using Counter() function.
- Traverse in this frequency dictionary and print all keys whose value is greater than 1.
Below is the implementation of above approach:
Python3
from collections import Counter
def printRepeating(arr, n):
freq = Counter(arr)
for i in freq:
if (freq[i] > 1 ):
print (i, end = " " )
arr = [ 12 , 10 , 9 , 45 ,
2 , 10 , 10 , 45 ]
n = len (arr)
printRepeating(arr, n)
|
C++
#include <iostream>
#include <unordered_map>
using namespace std;
void printRepeating( int arr[], int n)
{
unordered_map< int , int > freq;
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
for ( auto i : freq) {
if (i.second > 1) {
cout << i.first << " " ;
}
}
}
int main()
{
int arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 };
int n = sizeof (arr) / sizeof (arr[0]);
printRepeating(arr, n);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void printRepeating( int [] arr, int n)
{
Map<Integer, Integer> freq
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
int key = arr[i];
freq.put(key, freq.getOrDefault(key, 0 ) + 1 );
}
for ( int i : freq.keySet()) {
if (freq.get(i) > 1 ) {
System.out.print(i + " " );
}
}
}
public static void main(String[] args)
{
int [] arr = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 };
int n = arr.length;
printRepeating(arr, n);
}
}
|
complexity Analysis:
- Auxiliary Space: O(n), where n represents the size of the given array.
Method #3(Space Optimization) : we can use binary search lower_bound function to find first occurrence of arr[i] and Upper_bound function to find last occurrence of x and if the last_index-first_ind+1>1 means , arr[i] has more than one frequency.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printRepeating( int arr[], int n)
{ sort(arr,arr+n);
for ( int i=0;i <n ;i++)
{
int first_index = lower_bound(arr,arr+n,arr[i])- arr;
int last_index = upper_bound(arr,arr+n,arr[i])- arr-1;
int fre = last_index-first_index+1;
if (fre > 1 )
{ i=last_index;
cout<<arr[i]<< " " ; }
}
}
int main()
{
int arr[] = { 12, 10, 9, 45, 2, 10, 10, 45 };
int n = sizeof (arr) / sizeof (arr[0]);
printRepeating(arr, n);
return 0;
}
|
Java
import java.util.*;
class Main {
public static void printRepeating( int arr[], int n)
{
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++) {
int first_index = Arrays.binarySearch(arr, arr[i]);
int last_index = Arrays.binarySearch(arr, arr[i]);
if (first_index < 0 ) {
continue ;
}
while ((last_index < n - 1 ) && (arr[last_index + 1 ] == arr[i])) {
last_index++;
}
int fre = last_index - first_index + 1 ;
if (fre > 1 )
{
i = last_index;
System.out.print(arr[i] + " " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 };
int n = arr.length;
printRepeating(arr, n);
}
}
|
Python3
def printRepeating(arr, n):
arr.sort()
i = 0
while i < n:
first_index = i
last_index = i + arr.count(arr[i]) - 1
if last_index - first_index > 0 :
print (arr[i], end = " " )
i = last_index + 1
arr = [ 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 ]
n = len (arr)
printRepeating(arr, n)
|
Javascript
function printRepeating(arr, n) {
arr.sort();
let i = 0;
while (i < n) {
let first_index = i;
let last_index = i + arr.filter(x => x === arr[i]).length - 1;
if (last_index - first_index > 0) {
console.log(arr[i]);
}
i = last_index + 1;
}
}
let arr = [12, 10, 9, 45, 2, 10, 10, 45];
let n = arr.length;
printRepeating(arr, n);
|
Time Complexity: O(n*log2n), Take log2n for binary search function(upper and lower bound)
Auxiliary Space: O(1)
Please Login to comment...