Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.scalefocus;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String[] words = sc.nextLine().split(" ");
- LinkedHashMap<String, Integer> counts = new LinkedHashMap<>();
- for (String word : words) {
- String lowerWord = word.toLowerCase();
- if (!counts.containsKey(lowerWord)) {
- counts.put(lowerWord, 1);
- } else {
- counts.put(lowerWord, counts.get(lowerWord) + 1);
- }
- }
- // List<Integer> result = new ArrayList<>(); - if used with other type than String
- String result = counts.entrySet().stream()
- .filter(x -> x.getValue() % 2 != 0)
- .map(x->x.getKey()) // .getValue() for word counts
- // .collect(Collectors.toList()); - for other type of mappable (used in map) value
- .collect(Collectors.joining(", ")); //for strings only
- System.out.println(result);
- /*ArrayList<String> result = new ArrayList<>();
- for (Map.Entry<String, Integer> entry : counts.entrySet()) {
- if(entry.getValue() % 2 != 0) {
- result.add(entry.getKey());
- }
- }*/
- //System.out.println(String.join(", ", result));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement