-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathElectionLand.java
More file actions
36 lines (30 loc) · 887 Bytes
/
ElectionLand.java
File metadata and controls
36 lines (30 loc) · 887 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
package com.dtcc.exams.part5;
import java.util.Map;
import java.util.TreeMap;
public class ElectionLand {
public static Map<String, Integer> tally;
//Use tree for alphabetical sorting
public ElectionLand(){
tally = new TreeMap<>();
}
public String electionWinner(String[] votes){
String winner = null;
int tempCount = 0;
//Counts up votes for each
for(String temp:votes){
if(tally.containsKey(temp)){
tally.replace(temp, tally.get(temp) + 1);
}else{
tally.put(temp, 1);
}
}
//Get last with most
for(Map.Entry m: tally.entrySet()) {
if ((int)m.getValue() >= tempCount) {
tempCount = (int)m.getValue();
winner = (String)m.getKey();
}
}
return winner;
}
}