import java.util.*;
public class lianxi
{
public static void main(String[] args) {
HashMap<Integer,Integer> hsm=new HashMap<Integer,Integer>() ;
HashSet<Integer>maxzu=new HashSet<Integer>();//多个频数最高的的数字存放在一个hashset中
Scanner sca=new Scanner(System.in);
int x=sca.nextInt();
int keys=-999;
int max=0;
while(x!=-999)
{
if(hsm.get(x)!=null)
{
int count=hsm.get(x);
count++;
if(count>max)
{
maxzu.removeAll(maxzu);//换成新的
maxzu.add(x);
max=count;
}
if(count==max)
{
maxzu.add(x);//相同频率直接加入
}
hsm.put(x, count);
}
else
{
int chus=1;
hsm.put(x, chus);
if(chus>=max)
{maxzu.add(x);
max=chus;
}
}
x=sca.nextInt();
}
System.out.println(maxzu+" "+max);
}
}
hashmap找频数最高的数字
最新推荐文章于 2022-08-13 11:28:58 发布