Print all the Repeated Numbers with Frequency in an Array in C++

In this tutorial, we will learn how to print the repeated numbers with frequency in an array in C++. Before proceeding further First, Let’s understand the problem. What do you mean by repeated numbers?

Repeated Numbers: are the numbers who have occurred more than one time.

for example: In Set A= {3, 7, 2, 3, 9, 2, 3,3}

Elements 2 and 3 are repeated numbers. because they have occurred more than one time.

(*Note: We should not print the frequency of all the numbers in an array. We should print the frequency of only repeated numbers in an array.)




Frequency: refers to the no. of occurrences.

for example: In Set A= {3, 7, 2, 3, 9, 2, 3,3}

The frequency of element 3 is 4 (because the no of occurrences of element 3 in the given set is 4).

Similarly,

The frequency of elements 7, 2, 9 will be 1, 2, 1 respectively.

Now, let’s see the code for “Print all the Repeated Numbers with Frequency in an Array in C++”.

Program to print all the repeated numbers with frequency in an array in C++

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,j,n,count;

    // 'n' will contain the no. of elements.
    cout<<"Enter no. of elements:";
    cin>>n;

    // 'set[n]' will contain all the entered elements. 'set[n]' is an array of size n (as, we want to store only n elements).
    // Avoid writing set[50] or set[100] etc. By writing this we are wasting computer memory, use only that much amount of space which is required.

    int set[n];
    cout<<"\nEnter the elements:";
    for(i=0; i<n; i++)
    {
       cin>>set[i];
    }
    
    /* we want to print only 1 entry of repeated number. For this purpose, we have used 'flag[n]'. If flag[j] is equal to 1 then it means, we have already considered set[j] element for counting the frequency of a repeated number. If flag[j] is equal to 0 then it means we haven't considered set[j] element for counting the frequency of a repeated number.*/
    int flag[n]={0};          // Here, we have initialized all the blocks of the 'flag' array with 0.
    cout<<"\nRepeated Numbers with their frequency:";
    for(i=0; i<n; i++)
    {
      count=0;
      if(flag[i]!=1)       // if element set[i] is not considered for counting the frequency of a repeated number
      {
         for(j=0; j<n; j++)
         {
            if(set[i]==set[j])
            {
              count++;        
              flag[j]=1;     // set flag[j] to 1, to avoid more than 1 entry of repeated number in the output
            }
         }
         if(count>1)      // if no. of occurrences of element set[i] > 1 (or if the element set[i] is repeated number)
         {
           cout<<"\n"<<set[i]<<" -> "<<count;
         }
      }
    }
return 0;
}

Input/Output:

Enter no. of elements:10                                                                                                         
Enter the elements:4 7 6 4 8 4 7 2 1 1                                                                                                          
Repeated Numbers with their frequency:                                                                                           
4 -> 3                                                                                                                        
7 -> 2                                                                                                                        
1 -> 2

Time Complexity:

O(n^2), Where n is equal to no. of elements in the array.

 

You may also read:

  1. Check if a given matrix is a sparse matrix or not in C++
  2. Find trace and normal of a matrix in C++


One response to “Print all the Repeated Numbers with Frequency in an Array in C++”

  1. ranjit says:

    very helpful for beginners very good approach

Leave a Reply

Your email address will not be published. Required fields are marked *