title: Delete all matching pairs of a character from a string
body: <p>I'm trying to make a code that deletes the repeated characters. For example - if we have a string <code>"aabacdc"</code>, we want to make it as <code>"abd"</code>. If the character exists twice in the string, then we delete both characters as we did in the above example. The <code>'a'</code> occurs 3 times in our string, so we just deleted the 2 <code>a</code> and left 1 remaining.</p>
  
  <p>What I'm trying to do in this code is use two nested <code>for</code> loops - first for loop to compare the first character with the other characters. If the character has a duplicate in the string, then just delete both the characters. How can I fix this code?</p>
  
  <pre><code>public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      String str2 = input.nextLine();
  
      StringBuilder str = new StringBuilder(str2);
  
      for (int k = 0; k &lt; str.length() - 1; k++) {
          for (int i = 1; i &lt; str.length() - 1; i++) {
              if (str.charAt(k) == str.charAt(i)) {
                  str.deleteCharAt(k);
                  str.deleteCharAt(i);
              } 
          }
      }
  
      System.out.println(str);
  }
  </code></pre>