title: How to locate simple words amongst compound/simple words using Java?
body: <p>I have a list of words that have both 'simple' and 'compound' words in them, and would like to implement an algorithm that prints out a list of words without the compound words that are made up of the simple words.</p>
  
  <p>Sampel input:</p>
  
  <pre><code>chat, ever, snapchat, snap, salesperson, per, person, sales, son, whatsoever, what, so
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>chat, ever, snap, per, sales, son, what, so
  </code></pre>
  
  <p>I have written the following, but am stuck as to how to take it on from here:</p>
  
  <pre><code>private static String[] find(String[] words) {
  
      ArrayList&lt;String&gt; alist = new ArrayList&lt;String&gt;();
      Set&lt;String&gt; r1 = new HashSet&lt;String&gt;();
      for(String s: words){
          alist.add(s);
      }
      Collections.sort(alist,new Comparator&lt;String&gt;() {
  
          public int compare(String o1, String o2) {
  
              return o1.length()-o2.length();
          }
      });
  
      int count= 0;
      for(int i=0;i&lt;alist.size();i++){
          String check = alist.get(i);
          r1.add(check);
          for(int j=i+1;j&lt;alist.size();j++){
  
              String temp = alist.get(j);
              //System.out.println(check+" "+temp);
              if(temp.contains(check) ){
  
                  alist.remove(temp);
  
              }
          }
      }
      System.out.println(r1.toString());
      String res[] = new String[r1.size()];
      for(String i:words){
          if(r1.contains(i)){
              res[count++] = i;
          }
      }
  
      return res;
  }
  </code></pre>
  
  <p>Any guidance/insight or suggestions to a better approach would be appreciated.</p>