-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajority.java
More file actions
37 lines (33 loc) · 765 Bytes
/
Majority.java
File metadata and controls
37 lines (33 loc) · 765 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
Integer hasMajority(ArrayList<Integer> a){
HashMap<Integer, Integer> dict = new HashMap<Integer,Integer>();
int i = 0;
while(i < a.size()){
if(!dict.containsKey(i)){
dict.put(i, Collections.frequency(i));
}
if(100*dict.get(i)/a.size() > 50){
return a.get(i);
}
i++;
}
}
Integer hasMajority(ArrayList<Integer> a){
ArrayList<Integer> b = Collections.sort(a);
int maj = b.get(0);
int count = 1;
int count2 = 1; //holds actually maj count
while(i < b.size()){
if(b.get(i) == b.get(i-1)){
count++;
}
else{
if(count > count2){
count2 = count;
maj = b.get(i-1);
}
count = 1;
}
i++;
}
}
//Link: http://stackoverflow.com/questions/744981/array-of-size-n-with-one-element-n-2-times