public class Main{
// 用map来存储各个数据出现的次数
public static Map<Integer, Integer> map;
// 用number来存储数据的个数
public static int number;
// 数据初始化
public static void init(){
Scanner s = new Scanner(System.in);
map = new HashMap<Integer, Integer>();
number = s.nextInt();
s.nextLine();
for(int i=0; i<number; i++){
int temp = s.nextInt();
// 如果map中存在该数据
if(map.containsKey(temp)){
map.replace(temp, map.get(temp) + 1);
}
// 否则添加 (key, value) = (temp, 1
给定一个整型数组arr,请打印其中出现次数大于一半的数,如果没有这样的数,请输出-1
最新推荐文章于 2023-04-08 21:21:19 发布