Map map=new HashMap()
定义了一个集合,里面是多个 键 和 键对应的值。
可以简单想象成两个具有联系的数组:
一个数组存数(键)
一个数组存该数出现的次数(值)
参考代码:package Main;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class _1184 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int [] src=new int[n];
Map map=new HashMap();
for(int i=0;i
src[i]=sc.nextInt();
}
int value=0;
int max=0,t = 0;
for(int i=0;i
if(map.containsKey(src[i])){//函数 containsKey 是判断集合内是否有这个键
value=map.get(src[i]);//取这个键的值
value++;
map.put(src[i], value);//更新这个键的数据
if(value>max){
t=src[i];
max=value;
}
}
else{
map.put(src[i], 1);
}
}
System.out.println(t+"\n"+max);
}
}