-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWhoWonTheElection.java
More file actions
28 lines (22 loc) · 840 Bytes
/
Copy pathWhoWonTheElection.java
File metadata and controls
28 lines (22 loc) · 840 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
package com.smlnskgmail.jaman.codewarsjava.kyu6;
import java.util.*;
// https://www.codewars.com/kata/554910d77a3582bbe300009c
public class WhoWonTheElection {
private final List<String> input;
public WhoWonTheElection(List<String> input) {
this.input = input;
}
public String solution() {
Map<String, Integer> result = new HashMap<>();
input.forEach(b -> result.merge(b, 1, Integer::sum));
String winner = Collections.max(
result.entrySet(),
Comparator.comparingInt(Map.Entry::getValue)
).getKey();
int votesForWinner = result.get(winner);
if (result.values().stream().filter(i -> i == votesForWinner).count() > 1) {
return null;
}
return (votesForWinner <= input.size() / 2) ? null : winner;
}
}