Most Frequent Number in an Array

 Question:Find the most frequent element in the array in c++.

Approach:-

Step 1: Find the largest number in the array.

Step 2: Create a map (array) of the largest number plus 1.

Step 3: Store the count of each element in the map array and create a variable max_count, which stores the maximum count of the elements.

Step 4: Loop the map array and check whether there is an index whose value is equal to the max_count, and return that index because that index represents the most frequent element of the array. 

Code:-

#include<iostream>
#include<climits>
using namespace std;

int main(int argc, char const *argv[])
{
	int size;
	cout<<"Enter the size of array: "<<endl;
	cin>>size;
	int array[size];
	for(int i=0;i<size;i++){
		cin>>array[i];
	}
	//find the maximum value of the array...

	int max_value=INT_MIN;
	for(int i=0;i<size;i++){
		max_value=max(max_value,array[i]);
	} 

	//create an array having the size of max_value+1

	int map[max_value+1]={0};
    int max_count=0;
	for(int i=0;i<size;i++){
		int count=1;
		if(map[array[i]]==0){
			for(int j=i+1;j<size;j++){
				if(array[i]==array[j]){
					count++;
				}
			}
			map[array[i]]=count;
			max_count=max(max_count,count);
		}
	}
	//Now find the most frequent element having the map[index]==max_count return index;
	if(max_count==1){
		cout<<"There is no frequent element in the array";
	}
	else{
		for(int i=0;i<max_value+1;i++){
			if(map[i]==max_count){
				cout<<"Most Frequent Element: "<<i;
				break;
			}
	 	 }
	}
	return 0;
}

Comments