Advertisement
veronikaaa86

03. Odd Occurrences

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