Find the frequencies of all duplicates elements in the array
Last Updated :
21 Mar, 2023
Given an array of integers with duplicate elements in it, the task is to find the duplicate elements in the array and their frequencies.
Examples:
Input: arr[] = {2, 3, 4, 5, 4, 6, 4, 7, 4, 5, 6, 6}
Output: Below is the frequency of repeated elements –
4 –> 4
5 –> 2
6 –> 3
Input: arr[] = {4, 4, 5, 5, 6}
Output: Below is the frequency of repeated elements –
4 –> 2
5 –> 2
Approach:
- Create a Hash Map to store the frequency of the elements.
- Elements whose frequency is greater than 1 are the repeated elements.
Below is the implementation of the above approach:
CPP
#include<bits/stdc++.h>
using namespace std;
map< int , int > findRepeating( int arr[], int size){
map< int , int > frequency;
for ( int i = 0; i < size; i++)
frequency[arr[i]]++;
return frequency;
}
int main(){
int arr[] = {4, 4, 5, 5, 6};
int arr_size = sizeof (arr)/ sizeof (arr[0]);
map< int , int > frequency = findRepeating(arr, arr_size);
cout<< "Below is the frequency of repeated elements -" <<endl;
for ( auto x : frequency){
if (frequency[x.first] > 1)
cout<<x.first<< " --> " <<frequency[x.first]<<endl;
}
}
|
Java
import java.util.*;
class GFG
{
static HashMap<Integer, Integer> findRepeating( int []arr, int size){
HashMap<Integer,Integer> frequency = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < size; i++)
{
if (frequency.containsKey(arr[i]))
{
frequency.put(arr[i], frequency.get(arr[i]) + 1 );
}
else
{
frequency.put(arr[i], 1 );
}
}
return frequency;
}
public static void main(String []args)
{
int []arr = { 4 , 4 , 5 , 5 , 6 };
int arr_size = arr.length;
HashMap<Integer,Integer> frequency = findRepeating(arr, arr_size);
System.out.println( "Below is the frequency"
+ "of repeated elements -" );
for (Map.Entry<Integer,Integer> entry : frequency.entrySet())
if (entry.getValue() > 1 )
System.out.println(entry.getKey()+ " --> " +entry.getValue());
}
}
|
Python
def findRepeating(arr, size):
frequency = {}
for i in range ( 0 , size):
frequency[arr[i]] = \
frequency.get(arr[i], 0 ) + 1
return frequency
if __name__ = = "__main__" :
arr = [ 4 , 4 , 5 , 5 , 6 ]
arr_size = len (arr)
frequency = findRepeating(arr, arr_size)
print ("Below is the frequency\
of repeated elements - ")
for i in frequency:
if frequency[i] > 1 :
print (i, " --> " , frequency[i])
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static Dictionary< int , int > findRepeating( int []arr, int size){
Dictionary< int , int > frequency = new Dictionary< int , int >();
for ( int i = 0; i < size; i++)
{
if (frequency.ContainsKey(arr[i]))
{
frequency[arr[i]] = frequency[arr[i]] + 1;
}
else
{
frequency.Add(arr[i], 1);
}
}
return frequency;
}
public static void Main(String []args)
{
int []arr = {4, 4, 5, 5, 6};
int arr_size = arr.Length;
Dictionary< int , int > frequency = findRepeating(arr, arr_size);
Console.WriteLine( "Below is the frequency"
+ "of repeated elements -" );
foreach (KeyValuePair< int , int > entry in frequency)
if (entry.Value > 1)
Console.WriteLine(entry.Key+ " --> " +entry.Value);
}
}
|
Javascript
<script>
function findRepeating(arr, size)
{
var frequency = new Map();
for ( var i = 0; i < size; i++)
{
if (frequency.has(arr[i]))
{
frequency.set(arr[i], frequency.get(arr[i])+1);
}
else
{
frequency.set(arr[i], 1);
}
}
return frequency;
}
var arr = [4, 4, 5, 5, 6];
var arr_size = arr.length;
var frequency = findRepeating(arr, arr_size);
document.write( "Below is the frequency"
+ "of repeated elements -<br>" );
frequency.forEach((value, key) => {
if (value > 1)
document.write(key+ " --> " +value + "<br>" );
});
</script>
|
Output:
Below is the frequency of repeated elements -
4 --> 2
5 --> 2
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
Efficient Approach( Space optimization): we can use binary search . For this , First sort the array and then find frequency of all array element with the use of binary search function ( Upper_bound ) . The frequency of array element will be ‘last_index-first_index+1’ . If the frequency is greater than one , then print it .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findRepeating( 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;
i=last_index;
int fre = last_index-first_index+1;
if (fre >= 2)
{
cout << arr[i] << " --> " <<fre <<endl;
}
}
}
int main()
{
int arr[] = {2, 3, 4, 5, 4, 6, 4, 7, 4, 5, 6, 6};
int n = sizeof (arr)/ sizeof (arr[0]);
findRepeating(arr, n);
return 0;
}
|
C#
using System;
class MainClass {
static void FindRepeating( int [] arr, int n)
{
Array.Sort(arr);
for ( int i = 0; i < n; i++) {
int first_index = Array.IndexOf(arr, arr[i]);
int last_index = Array.LastIndexOf(arr, arr[i]);
i = last_index;
int fre = last_index - first_index + 1;
if (fre >= 2) {
Console.WriteLine(arr[i] + " --> " + fre);
}
}
}
public static void Main( string [] args)
{
int [] arr = { 2, 3, 4, 5, 4, 6, 4, 7, 4, 5, 6, 6 };
int n = arr.Length;
FindRepeating(arr, n);
}
}
|
Java
import java.util.*;
class Main {
public static void findRepeating( int arr[], int n)
{
Arrays.sort(arr);
int i = 0 ;
while (i < n) {
int firstIndex = i;
while (i < n - 1 && arr[i] == arr[i + 1 ]) {
i++;
}
int lastIndex = i;
int frequency = lastIndex - firstIndex
+ 1 ;
if (frequency >= 2 ) {
System.out.println(arr[i] + " --> "
+ frequency);
}
i++;
}
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 5 , 4 , 6 , 4 , 7 , 4 , 5 , 6 , 6 };
int n = arr.length;
findRepeating(arr, n);
}
}
|
Javascript
function findRepeating(arr, n) {
arr.sort();
for (let i = 0 ; i < n ; i++) {
let first_index = arr.indexOf(arr[i]);
let last_index = arr.lastIndexOf(arr[i]);
i = last_index;
let fre = last_index - first_index + 1;
if (fre >= 2) {
console.log(arr[i] + " --> " + fre);
}
}
}
let arr = [2, 3, 4, 5, 4, 6, 4, 7, 4, 5, 6, 6];
let n = arr.length;
findRepeating(arr, n);
|
Python3
def find_repeating(arr, n):
arr.sort()
i = 0
while i < n:
first_index = i
while i < n - 1 and arr[i] = = arr[i + 1 ]:
i + = 1
last_index = i
frequency = last_index - first_index + 1
if frequency > = 2 :
print ( str (arr[i]) + " --> " + str (frequency))
i + = 1
arr = [ 2 , 3 , 4 , 5 , 4 , 6 , 4 , 7 , 4 , 5 , 6 , 6 ]
n = len (arr)
find_repeating(arr, n)
|
Output
4 --> 4
5 --> 2
6 --> 3
Time Complexity: O(n*log2n) , Taking O(log2n) time for binary search function ( upper_bound) .
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...