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];
}
}