forked from haman2306/hacktoberfest2k20
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajority_Element.cpp
More file actions
58 lines (37 loc) · 1.04 KB
/
Copy pathMajority_Element.cpp
File metadata and controls
58 lines (37 loc) · 1.04 KB
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
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
int majorityWins(int arr[], int n, int x,int y);
int majorityWins(int arr[], int n, int x,int y)
{
int count_x=0;//counter to count frequency of x
int count_y=0;//counter to count frequency of y
for(int i=0;i<n;i++)
{
if(arr[i]==x)
count_x++;
else if(arr[i]==y)
count_y++;
}
if(count_x>count_y)
return x;
else if(count_y>count_x)
return y;
else
return x<y?x:y;
}
int main() {
int t; //Testcases
cin>>t; // Input the testcases
while(t--) //Until all testcases are exhausted
{
int n; //Size of array
cin>>n; //Input the size
int arr[n]; //Declaring array of size n
for(int i=0;i<n;i++) // Running a loop to input all elements of arr
cin>>arr[i];
int x,y; //declare x and y
cin>>x>>y; // input x and y
cout << majorityWins(arr,n,x,y) << endl; //calling the function that you complete
}
return 0;
}