title: How to remove duplicate character from a string in java?
body: <p>In my program, the user enters a string, and it first finds the largest mode of characters in the string. Next, my program is supposed to remove all duplicates of a character in a string, (user input: aabc, program prints: abc) which I'm not entirely certain on how to do. I can get it to remove duplicates from some strings, but not all. For example, when the user puts "aabc" it will print "abc", but if the user puts "aabbhh", it will print "abbhh." Also, before I added the removeDup method to my program, it would only print the maxMode once, but after I added the removeDup method, it began to print the maxMode twice. How do I keep it from printing it twice?</p>
  
  <p>Note: I cannot convert the strings to an array.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class JavaApplication3 {
  static class MyStrings {
      String s;
  
  void setMyStrings(String str) {
      s = str;    
  }
  
  int getMode() {
          int i;
          int j;
          int count = 0;
          int maxMode = 0, maxCount = 1;
          for (i = 0; i&lt; s.length(); i++) {
              maxCount = count;
              count = 0;
              for (j = s.length()-1; j &gt;= 0; j--) {
                  if (s.charAt(j) == s.charAt(i))
                      count++;
                  if (count &gt; maxCount){
                      maxCount = count;
                      maxMode = i;
                  }
              }       
          }
          System.out.println(s.charAt(maxMode)+" = largest mode");
  
        return maxMode;  
  }
  
      String removeDup() {
         getMode();
         int i;
         int j;
         String rdup = "";
  
         for (i = 0; i&lt; s.length(); i++) {
             int count = 1;
             for (j = 0; j &lt; rdup.length(); j++) {
                 if (s.charAt(i) == s.charAt(j)){
                      count++;
                 }
             }
             if (count == 1){
                 rdup += s.charAt(i);
                 }
             } 
             System.out.print(rdup);
             System.out.println();
             return rdup;
         }
  
  
  }
  
  public static void main (String[] args) {
  Scanner in = new Scanner(System.in);
  MyStrings setS = new MyStrings();
  
      String s;
  
              System.out.print("Enter string:");
              s = in.nextLine();
  
              setS.setMyStrings(s);
              setS.getMode();
              setS.removeDup();
  
  
  }
  
  }
  </code></pre>