构建哈希图后,您可以构建另一个,其中键是映射编号,值可能是频率。然后所有要做的就是找到最大频率。
HashMap<Integer,Integer> freq_map = new HashMap<Integer,Integer>();
int map_no=0,fr;
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
map_no = pairs.getValue());
if(map.contains(map_no)) fr = freq_map.get(map_no);
else fr = 0;
fr ++;
freq_map.put(map_no,fr);
}
// to find max
int max = -1,temp;
int result = -1;
Iterator it = freq_map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
temp = pairs.getValue());
if(temp>max){
max = temp;
result = pairs.getKey();
}
}
System.out.println("Highest frequency = "+result);