输出数组内每个数字及出现的次数
public static void main(String[] args) {
int[] array = {1,4,7,3,5,3,2,8,7,33,4,12,3,23,5,4,7,8,9,8,9,6,4,3,2};
HashSet<Integer> set = new HashSet<>();
Map<Integer, Integer> map = new HashMap<>();
for(int i=0; i<array.length; i++){
if(!set.add(array[i])){
map.put(array[i], map.get(array[i]) + 1);
} else {
map.put(array[i], 1);
}
}
for(Integer key : map.keySet()){
System.out.println("数字" + key + "出现" + map.get(key)+ "次");
}
}
利用该方法可做其他变形,比如找出数组中重复的数字及出现的次数,剔除数组中重复的数字等等