Given a string[] lines, each line will have an ip address , find the ip address with the highest frequency.
The given data only has one IP with the highest frequency
lines = ["192.168.1.1","192.118.2.1","192.168.1.1"]
return "192.168.1.1"
public class Solution {
/**
* @param ipLines: ip address
* @return: return highestFrequency ip address
*/
public String highestFrequency(String[] ipLines) {
Map<String, Integer> map = new HashMap<String, Integer>();
String ans = "";
int count = 0;
for (String str: ipLines) {
Integer occur = map.get(str);
if (occur != null) {
occur += 1;
} else {
occur = 1;
}
map.put(str, occur);
if (occur > count) {
count = occur;
ans = str;
}
}
return ans;
}
}
描述
给定一个字符串数组lines, 每一个元素代表一个IP地址,找到出现频率最高的IP。
给定数据只有一个频率最高的IP
您在真实的面试中是否遇到过这个题?
样例
lines = ["192.168.1.1","192.118.2.1","192.168.1.1"]
return "192.168.1.1"
原文:https://www.cnblogs.com/browselife/p/10646008.html