1613. 最高频率的IP
给定一个字符串数组lines
, 每一个元素代表一个IP地址,找到出现频率最高的IP。
样例
样例1:
输入 = ["192.168.1.1","192.118.2.1","192.168.1.1"]
输出 "192.168.1.1"
样例2:
输入 = ["192.168.1.1","192.118.2.1","192.168.1.1","192.118.2.1","192.118.2.1"]
输出 "192.118.2.1"
注意事项
给定数据只有一个频率最高的IP
public class Solution {
/**
* @param ipLines: ip address
* @return: return highestFrequency ip address
*/
public String highestFrequency(String[] ipLines) {
// Write your code here
int a[] = new int[ipLines.length];
for(int i =0;i<ipLines.length;i++){
int sum = 0;
for(int j = 0;j<ipLines.length;j++){
if(ipLines[i].equals(ipLines[j])){
sum++;
}
}
a[i] = sum;
//System.out.println(sum);
}
String ip;
int temp;
for(int x=0;x<ipLines.length;x++){
for(int y=x+1;y<ipLines.length;y++){
if(a[x]>=a[y]){
ip = ipLines[x];
ipLines[x] = ipLines[y];
ipLines[y] = ip;
temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
}
for(int z=0;z<a.length;z++)
System.out.print(a[z]+" ");
return ipLines[ipLines.length-1];
}
}