-
Notifications
You must be signed in to change notification settings - Fork 0
/
MajorityElement1.cpp
51 lines (49 loc) · 953 Bytes
/
MajorityElement1.cpp
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
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int Case, n, index = 0;
vector<int> c;
cin >> Case;
while (Case > 0)
{
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int maxCount = 0;
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
if (a[i] == a[j])
{
count++;
}
}
if (count > maxCount)
{
maxCount = count;
index = i;
}
}
if (maxCount > n / 2)
{
c.push_back(a[index]);
}
else
{
c.push_back(-1);
}
Case--;
}
for (auto i = c.begin(); i != c.end(); i++)
{
cout << *i << endl;
}
return 0;
}