Didart

Odd Occurrences

Nov 4th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class OddOccurrences {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         String[] sequence = scanner.nextLine().split(" ");
  9.  
  10.         LinkedHashMap<String, Integer> count = new LinkedHashMap<>();
  11.  
  12.         for (String sequenceOfWords : sequence) {
  13.             String wordLowerCase = sequenceOfWords.toLowerCase(Locale.ROOT);
  14.             if (count.containsKey(wordLowerCase)) {
  15.                 count.put(wordLowerCase, count.get(wordLowerCase) + 1);
  16.             } else {
  17.                 count.put(wordLowerCase, 1);
  18.             }
  19.         }
  20.  
  21.         ArrayList<String> oddElement = new ArrayList<>();
  22.  
  23.         for (var entry : count.entrySet()) {
  24.             if (entry.getValue() % 2 == 1) {
  25.                 oddElement.add(entry.getKey());
  26.             }
  27.         }
  28.        
  29.         for (int i = 0; i < oddElement.size(); i++) {
  30.             System.out.print(oddElement.get(i));
  31.             if (i < oddElement.size() - 1) {
  32.                 System.out.print(", ");
  33.             }
  34.         }
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment