HDACM 2192
这一题的本质是找众数的重数,采用简单遍历会超时,所以我用到Java里面的map集合,map是以键值对的形式存在,一个键只能对应一个值,这样我们就可以遍历一遍找到众数的重数了。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int t = sc.nextInt();while (t-- > 0) {int n = sc.nextInt();Map<Integer, Integer> m = new HashMap<Integer, Integer>();int b[] = new int[n];for (int i = 0; i < b.length; i++) {b[i] = sc.nextInt();m.put(b[i], 0);}int max = 0;for (int i = 0; i < b.length; i++) {int value =m.get(b[i])+1;if (value>max) {max = value;}m.put(b[i], value);}System.out.println(max);}sc.close();}
}