Advertisement
veronikaaa86

03. Odd Occurrences

Nov 3rd, 2021
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. package mapAndStreamAPI;
  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[] wordsArr = scanner.nextLine().split(" ");
  10.  
  11. Map<String, Integer> countWordsMap = new LinkedHashMap<>();
  12.  
  13. for (String word : wordsArr) {
  14. String wordToLower = word.toLowerCase();
  15.  
  16. if (countWordsMap.containsKey(wordToLower)) {
  17. int currentCount = countWordsMap.get(wordToLower);
  18. countWordsMap.put(wordToLower, currentCount + 1);
  19. } else {
  20. countWordsMap.put(wordToLower, 1);
  21. }
  22. }
  23.  
  24. List<String> oddCountWords = new ArrayList<>();
  25.  
  26. for (Map.Entry<String, Integer> entry : countWordsMap.entrySet()) {
  27. int count = entry.getValue();
  28. if (count % 2 != 0) {
  29. oddCountWords.add(entry.getKey());
  30. }
  31. }
  32.  
  33. System.out.println(String.join(", ", oddCountWords));
  34. }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement