TheFestFever.com

Our aim is to connect students from campuses all over Delhi by making them aware of all Technical, Cultural, Management fests, workshops, Seminars organized by every college in Delhi.

Join Us Now

Program 52: Write a C++ program to print the frequency of each unique element in an array.

#include<iostream>
using namespace std;

void dublicate(int x[],int &size)
{
	for(int i=0;i<size;i++)
	{
		for(int j=i+1;j<size;j++)
		{
			if(x[i]==x[j])
			{
				for(int k=j+1;k<size;k++)
				{
					x[k-1]=x[k];
				}
				size--;
			}
		}
	}
}

int main()
{
	int size;
	cout<<"\nenter the size of an array : ";
	cin>>size;
	int size1,arr1[size],arr[size];
	cout<<"\nenter an array : ";

	for(int i=0;i<size;i++)
	{
		cin>>arr[i];
		arr1[i]=arr[i];
	}
	dublicate(arr1,size1);
	cout<<"\nfrequency of each unique element : ";

	for(int i=0;i<size1;i++)
	{
		int count=0;
		for(int j=0;j<size;j++)
		{
			if(arr1[i]==arr[j])
			count++;
		}
		cout<<"\n"<<arr1[i]<<" : "<<count;
	}
	return 0;
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.