forked from Yashkothari9/CPP-CODES
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMajority_vote.cpp
More file actions
53 lines (49 loc) · 970 Bytes
/
Majority_vote.cpp
File metadata and controls
53 lines (49 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int g=a[0];
for(int h=1;h<n;h++)
{
if(g<a[h])
{
int temp=g;
g=a[h];
a[h]=temp;
}
}
int *b=new int[g+1];
for(int j=0;j<=g;j++)
{
b[j]=0;
}
for(int k=0;k<n;k++)
{
b[a[k]]++;
}
int maxi=0;
int mac=b[0];
for(int y=1;y<=g;y++)
{
if(mac<b[y])
{
int temp=maxi;
maxi=y;
y=temp;
mac=b[y];
}
}
cout<<maxi<<"\n";
}
}