title: How to remove duplicate words from a text file using java
body: <p>I have a input file like</p>
  
  <p>names.txt </p>
  
  <pre><code>   Maria Derek Erica
     Livia Jack Anita
     Kendall Maria Livia Derek
     Jamie Jack
     Thomson Erica
  
  </code></pre>
  
  <p>I want to output like. Removing duplicates words from the name</p>
  
  <p>output.txt</p>
  
  <pre><code>   Maria Derek Erica
     Livia Jack Anita
     Kendall 
     Jamie 
     Thomson
  
  </code></pre>
  
  <p>I already tried to read file separated by whitespace and then add them into ArrayList then I lost what to do next to generate output.</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.util.Scanner;
  import java.io.*;
  import java.util.ArrayList;
  
  public class duplicate {
     public static void main(String[] args) throws IOException {
        ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
        File file = new File("weather.txt");
        Scanner input = new Scanner(file); 
        String prev = input.next();
        int count = 0;
        while (input.hasNext()) {
           String next  = input.next();
           System.out.println(next);
           set.add(next);
           count = count + 1;
           if(prev.equals(next))
           {
              System.out.println("Match found: -" + prev);
           }
           prev = next;
        } 
  
        System.out.println(list);
        System.out.println("Word count: " + count);
     }
  }
  </code></pre>