BetinaUKTC

zadacha4

Mar 31st, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. package Prog;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9. public static void fillArray(HashMap<Integer, Integer> map, int[] num) {
  10. for (int val : num) {
  11. if (map.containsKey(val)) {
  12. int count = map.get(val) + 1;
  13. map.put(val, count);
  14. } else {
  15. map.put(val, 1);
  16. }
  17. }
  18. }
  19.  
  20. public static void print(HashMap<Integer, Integer> map) {
  21. for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  22. System.out.println(entry.getKey() + " -> " + entry.getValue());
  23. }
  24. }
  25.  
  26. public static void main(String[] args) {
  27. Scanner scan = new Scanner(System.in);
  28.  
  29. String[] input = scan.nextLine().split(" ");
  30. int[] num = new int[input.length];
  31. for (int i = 0; i < input.length; i++) {
  32. num[i] = Integer.parseInt(input[i]);
  33. }
  34.  
  35. HashMap<Integer, Integer> map = new HashMap<>();
  36. fillArray(map, num);
  37. print(map);
  38.  
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment