Java Regex 2 - Duplicate Words

  • + 0 comments

    Please advise on why test case fails even though output matches. I believe its an minor mistake.

    public class DuplicateWords {

    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        int numSentences = Integer.parseInt(in.nextLine());
    
        while(numSentences > 0)
        {
            String input = in.nextLine();
            List<String> list = Arrays.asList(input.split(" "));
            Set<String> ans = new HashSet<>();  
            String answer = list.stream()
            .filter(str -> ans.add(str.toLowerCase()))
            .collect(Collectors.joining(" ")).trim();        
            System.out.println(answer);
            numSentences--;
        }
    
        in.close();
    }
    

    }