TopK问题
import java.util.HashMap;
/**
* 在2.5亿个正整数中找出不重复的整数。
*/
public class TopK {
public static void search(String limitText){
String maxWord = "";
int maxTime = 0;
String[] words = limitText.split(" |\\.|,");
int length = words.length;
HashMap<String,Integer> one = new HashMap<>();
for(int j=0;j<length;j++){
Integer number = one.get(words[j]);
if(number != null){
number = number + 1;
/** 找到次数加 1 */
one.put(words[j],number);
if(maxTime < number){
maxTime = number;
maxWord = words[j];
}
}else{
/** 没找到,赋值 1 */
one.put(words[j],1);
}
}
System.out.println("maxTime is :"+maxTime+" ; maxWord is :"+maxWord);
}
}