Sunday, 12 November 2017

C++ Program to find the Majority element of the Array

C++ Program to find the majority element of the array is given below. The element which occurs more than n/2 times in the array is said to be the majority element. Approach is very simple count the occurrence of each element and then check if it is occurred more than n/2 times or not.


PROGRAM:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;

int main() {
	//code
	cout<<"\nEnter the number of test cases: \n";
	int t;
	cin>>t;
	while(t--){


	    int n,result,flag=0;
	    cout<<"\nEnter the number of elements: \n";
	    cin>>n;
	    std::unordered_map<int,int> m;
	    int num;

	    cout<<"\nEnter the elements of array: \n";
	    for(int i=0;i<n;i++){
	        cin>>num;
	        m[num]++;
	        if(m[num]>n/2){ result=num; flag=1;}
	    }
	    if(flag){
	       cout<<"\nThe Majority element of the array is: \n";
	       cout<<result;
	    }else
	    {
	        cout<<"NO Majority Element";
	    }
	    cout<<"\n";
	}
	return 0;
}


OUTPUT:



EmoticonEmoticon