-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorElement.cpp
More file actions
50 lines (46 loc) · 824 Bytes
/
Copy pathMajorElement.cpp
File metadata and controls
50 lines (46 loc) · 824 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int countMajor(vector<int>& v)
{
long max = (v.size() / 2);
long c = 0;
int e = 0;
do{
e = v[0];
c = count(v.begin(), v.end(), v[0]);
if (c > max)
return v[0];
v.erase(remove_if(v.begin(), v.end(), [e](int i){
return (e == i);
}), v.end());
} while (!v.empty());
return -1;
}
int main(int argc, const char * argv[])
{
string token;
ifstream ifs(argv[1]);
vector<int> vs;
stringstream ss;
while (getline(ifs, token))
{
ss << token;
while (getline(ss,token,','))
{
vs.push_back(stoi(token));
}
int ans = countMajor(vs);
if (ans == -1)
cout << "NONE" << endl;
else
cout << ans << endl;
ss.clear();
vs.clear();
}
system("pause");
}