Advertisement
Ligh7_of_H3av3n

04. Count Real Numbers

May 20th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. package Lekciq;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class CountRealNumbers {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         String input = scanner.nextLine();
  13.         String[] numbers = input.split(" ");
  14.  
  15.         // Map to store the count of each number
  16.         Map<Double, Integer> countMap = new LinkedHashMap<>();
  17.  
  18.         // Iterate over the numbers array and count occurrences
  19.         for (String number : numbers) {
  20.             double num = Double.parseDouble(number);
  21.             countMap.put(num, countMap.getOrDefault(num, 0) + 1);
  22.         }
  23.  
  24.         // Print the result
  25.         for (Map.Entry<Double, Integer> entry : countMap.entrySet()) {
  26.             double num = entry.getKey();
  27.             int count = entry.getValue();
  28.             System.out.printf("%.1f -> %d\n", num, count);
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement