Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Prog;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class Main {
- public static void fillArray(HashMap<Integer, Integer> map, int[] num) {
- for (int val : num) {
- if (map.containsKey(val)) {
- int count = map.get(val) + 1;
- map.put(val, count);
- } else {
- map.put(val, 1);
- }
- }
- }
- public static void print(HashMap<Integer, Integer> map) {
- for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
- System.out.println(entry.getKey() + " -> " + entry.getValue());
- }
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] input = scan.nextLine().split(" ");
- int[] num = new int[input.length];
- for (int i = 0; i < input.length; i++) {
- num[i] = Integer.parseInt(input[i]);
- }
- HashMap<Integer, Integer> map = new HashMap<>();
- fillArray(map, num);
- print(map);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment