Advertisement
IrinaIgnatova

Odd Occurrences

Jul 16th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String[] words = scanner.nextLine().split(" ");
  14.  
  15.         LinkedHashMap<String, Integer> counts = new LinkedHashMap<>();
  16.         for (String word : words) {
  17.             String wordsOnLowerCAse = word.toLowerCase();
  18.             if (counts.containsKey(wordsOnLowerCAse)) {
  19.                 counts.put(wordsOnLowerCAse, counts.get(wordsOnLowerCAse) + 1);
  20.             } else {
  21.                 counts.put(wordsOnLowerCAse, 1);
  22.             }
  23.         }
  24.         ArrayList<String> odds = new ArrayList<>();
  25.  
  26.         for (Map.Entry<String, Integer> entry : counts.entrySet()) {
  27.             if (entry.getValue() % 2 == 1) {
  28.                 odds.add(entry.getKey());
  29.             }
  30.         }
  31.  
  32.         for (int i = 0; i < odds.size(); i++) {
  33.             System.out.print(odds.get(i));
  34.             if (i < odds.size() - 1) {
  35.                 System.out.print(", ");
  36.             }
  37.         }
  38.  
  39.  
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement