EmiliaKitkarska

Zadacha4

Apr 19th, 2021
621
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 4. Брой на числа
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Zadachki {
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×