Skip to content
Related Articles

Related Articles

Improve Article

Find the frequencies of all duplicates elements in the array

  • Difficulty Level : Medium
  • Last Updated : 01 Jul, 2021

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




// CPP Implementation to find the
// repeating elements with there count
#include<bits/stdc++.h>
using namespace std;
 
// Function to find the repeating
// elements with there count
map<int,int> findRepeating(int arr[], int size){
 
    // Hash map to store the
    // frequency of elements
    map<int,int> frequency;
     
    // Loop to store the frequency of
    // elements of array
    for (int i = 0; i < size; i++)
    frequency[arr[i]]++;
    return frequency;
}
         
// Driver Code
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;
    }
}
     
// This code is contributed by Surendra_Gangwar

Java




// Java Implementation to find the
// repeating elements with there count
import java.util.*;
 
class GFG
{
     
// Function to find the repeating
// elements with there count
static HashMap<Integer, Integer> findRepeating(int []arr, int size){
     
    // Hash map to store the
    // frequency of elements
    HashMap<Integer,Integer> frequency = new HashMap<Integer,Integer>();
     
    // Loop to store the frequency of
    // elements of array
    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;
}
 
// Driver Code
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());
}
}
 
// This code is contributed by PrinciRaj1992

Python




# Python Implementation to find the
# repeating elements with there count
 
# Function to find the repeating
# elements with there count
def findRepeating(arr, size):
     
    # Hash map to store the
    # frequency of elements
    frequency = {}
     
    # Loop to store the frequency of
    # elements of array
    for i in range (0, size):
        frequency[arr[i]] = \
        frequency.get(arr[i], 0) + 1
    return frequency
     
# Driver Code
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#




// C# Implementation to find the
// repeating elements with there count
using System;
using System.Collections.Generic;
 
class GFG
{
     
// Function to find the repeating
// elements with there count
static Dictionary<int, int> findRepeating(int []arr, int size){
     
    // Hash map to store the
    // frequency of elements
    Dictionary<int,int> frequency = new Dictionary<int,int>();
     
    // Loop to store the frequency of
    // elements of array
    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;
}
 
// Driver Code
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);
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
// Javascript Implementation to find the
// repeating elements with there count
     
// Function to find the repeating
// elements with there count
function findRepeating(arr, size)
{
     
    // Hash map to store the
    // frequency of elements
    var frequency = new Map();
     
    // Loop to store the frequency of
    // elements of array
    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;
}
 
// Driver Code
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>");
});
 
// This code is contributed by rrrtnx.
</script>
Output: 
Below is the frequency    of repeated elements -
4  -->  2
5  -->  2

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :