title: Getting each letter in a String only once
body: <p>I'm writing a Java program for Horspool's algorithm, and am having a bit of trouble.  I'm trying to create an array of chars that will hold each letter in a string, but I don't want duplicates of the letters.  Right now this is my code:</p>
  
  <pre><code>public static void main(String[] args) 
  {
      Scanner scanIn = new Scanner (System.in);
  
      int count = 0;
  
      int count2 = 0;
  
      int inc = 0;
  
      //The text to search for the phrase in
      String t = "";
  
      //The phrase/pattern to search for
      String p = "";
  
      System.out.println(" ");
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
      System.out.println("Harspool's Algorithm: ");
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
      System.out.println(" ");
      System.out.println("Please enter the full text: ");
      t = scanIn.nextLine();
      System.out.println("Please enter the pattern to search for: ");
      p = scanIn.nextLine();
  
      char[] text = new char[t.length()];
      char[] pattern = new char[p.length()];
      char[] alphabet = new char[t.length()];
  
      for (int i = 0; i &lt; alphabet.length; i++)
      {
          alphabet[i] = ' ';
      }
  
  
      for (int i = 0; i &lt; text.length; i++)
      {
          text[i] = t.charAt(i);
      }
  
      for (int i = 0; i &lt; pattern.length; i++)
      {
          pattern[i] = p.charAt(i);
      }
  
      while (inc &lt; text.length)
      {
          for (int j = 0; j &lt; text.length; j++)
          {
              if (text[inc] != alphabet[j])
              {
                  count++;
              }
              if (count == p.length() - 1 &amp;&amp; count2 &lt; text.length)
              {
                  alphabet[count2] = text[inc];
                  count2++;
                  count = 0;
                  inc++;
              }
          }
      }
  
      for (int i = 0; i &lt; alphabet.length; i++)
      {
          System.out.print(alphabet[i]);
      }
  }
  </code></pre>
  
  <p>I believe the problem is in the while loop, but I can't figure out what exactly is going wrong. Right now, it will print out the entire string passed in, when it should be printing each letter only once.  Could someone please help?</p>