3.使用Map键值对

3.使用Map键值对_第1张图片

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Map<Integer,Integer> map = new HashMap<>();
		for(int i=0;i<n;i++) {
			int a = sc.nextInt();
			map.put(a, map.getOrDefault(a, 0)+1);
		}
		int max = Integer.MIN_VALUE;
		for(Entry<Integer, Integer> entry : map.entrySet()) {
			max = Math.max(max,entry.getValue());			
		}
		List<Integer> list = new ArrayList<>();
		for(Entry<Integer, Integer> entry : map.entrySet()) {
			if(max == entry.getValue())
				list.add(entry.getKey());
		}
		Collections.sort(list);
		for(int x:list)
			System.out.print(x+" ");		
	}
}

你可能感兴趣的:(蓝桥杯省赛(Java组),java)