int[] nums =new int[]{2,2,3,2}; HashMap<Integer, Integer> hashMap = new HashMap<>(); for (int num : nums) { hashMap.put(num, hashMap.getOrDefault(num, 0) + 1); }