华为笔试 投票排序
Description
输入:Tom,Tom,Lucy,Lucy,Jack
输出:Lucy
解释:由于Tom和Lucy的得票数一样,但是a>b>c,A>B>C,Tom>Tomy;由于L>T,所以Lucy为第一。
输入:Tom,Tom,Lucy
输出:Tom
Solution
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class lexical {
public static void main(String[] args) {
HashMap<String,Integer> map = new HashMap<>();
int counter = 0;
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] arrs = s.split(",");
for(String vote : arrs){
if(map.containsKey(vote)){
counter = (Integer)map.get(vote);
map.put(vote,++counter);
}else{
map.put(vote,1);
}
}
List<Map.Entry<String,Integer>> teamList = new ArrayList<>(map.entrySet());
int max=0;
for(int i=0;i<teamList.size();i++){
if(teamList.get(i).getValue()>max){
max = teamList.get(i).getValue();
}
}
List<String> result = new ArrayList<>();
for(int i=0;i<teamList.size();i++){
if(teamList.get(i).getValue()==max){
result.add(teamList.get(i).getKey());
}
}
if(result.size()==1)System.out.println(result.get(0));
else{
Collections.sort(result);
System.out.println(result.get(0));
}
}
}