Advertisement
veronikaaa86

03. Odd Occurrences

Jul 6th, 2022
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 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[] lineInput = scanner.nextLine().split(" ");
  10.  
  11. Map<String, Integer> wordsCountMap = new LinkedHashMap<>();
  12. for (int i = 0; i < lineInput.length; i++) {
  13. String currentWord = lineInput[i].toLowerCase();
  14.  
  15. Integer count = wordsCountMap.get(currentWord);
  16. if (wordsCountMap.containsKey(currentWord)){
  17. wordsCountMap.put(currentWord, count + 1);
  18. } else {
  19. wordsCountMap.put(currentWord, 1);
  20. }
  21. }
  22.  
  23. List<String> oddWords = new ArrayList<>();
  24. for (Map.Entry<String, Integer> entry : wordsCountMap.entrySet()) {
  25. if (entry.getValue() % 2 == 1) {
  26. oddWords.add(entry.getKey());
  27. }
  28. }
  29.  
  30. System.out.println(String.join(", ", oddWords));
  31. }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement