Programming Examples

Cpp program to find the frequency of elements in an array


Write a cpp program to find the frequency of elements in an array

Solution

#include<iostream>
using namespace std;
int main(){
    int num[5],a,b,flag;
    cout<<"Enter 5 elements in an array";
    for(a=0;a<5;a++)
    {
        cin>>num[a];
    }
    for(a=0;a<5;a++)
    {    
        flag=0;
        for(b=a-1;b>=0;b--) 
        {
            if(num[a]==num[b])
            {
                 flag=1;
                 break;
            } 
        }
        if(flag==0)
        {
            int count=0;  
            for( b=0;b<5;b++)
            {
                if(num[a]==num[b])
                 {
                     count++;
                }
            } 
            cout<<num[a]<<" -> "<<count<<endl;        
        }
    }
    return 0;
}
Output

Enter 5 elements in an array 

1

1

2

2

3

1 -> 2

2 -> 2

3 -> 1


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz