Advertisement
veronikaaa86

03. Odd Occurrences

Mar 1st, 2023
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. package associativeArrays;
  2.  
  3. import java.util.*;
  4.  
  5. public class P03OddOccurrences {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] inputArr = scanner.nextLine().split(" ");
  10.  
  11.         LinkedHashMap<String, Integer> wordsMap = new LinkedHashMap<>();
  12.         for (String item : inputArr) {
  13.             item = item.toLowerCase();
  14.  
  15.             wordsMap.putIfAbsent(item, 0);
  16.             wordsMap.put(item, wordsMap.get(item) + 1);
  17.  
  18. //            if (!wordsMap.containsKey(item)) {
  19. //                wordsMap.put(item, 1);
  20. //            } else {
  21. //                int value = wordsMap.get(item);
  22. //                wordsMap.put(item, value + 1);
  23. //            }
  24.         }
  25.  
  26.         List<String> resultList = new ArrayList<>();
  27.         for (Map.Entry<String, Integer> entry : wordsMap.entrySet()) {
  28.             if (entry.getValue() % 2 != 0) {
  29.                 resultList.add(entry.getKey());
  30.             }
  31.         }
  32.         System.out.println(String.join(", ", resultList));
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement