Java Regex 2 - Duplicate Words

  • + 1 comment

    Same with me

    import java.util.Scanner;
    
    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();    
            String[] arr = input.split(" ");
            String[] res = new String [arr.length];
            int i = 0;
            for(String item : arr ){                
                boolean found = false;
                    
                for (String  term :  res )
                    if ( term != null && term.equalsIgnoreCase(item))
                        found = true;
                    
                if ( found == false )
                    res[i] = item;
                i++;
            }
            
            input = "";        
            for (String  term : res )
                if (term != null)
                    input += term + " ";
                
            System.out.println(input);
        }
        in.close();
    }
    }