title: Removing duplicate same characters in a row
body: <p>I am trying to create a method which will either remove all duplicates from a string or only keep the same 2 characters in a row based on a parameter.</p>
  
  <p>For example:</p>
  
  <pre><code>helllllllo -&gt; helo
  </code></pre>
  
  <p>or</p>
  
  <p><code>helllllllo -&gt; hello</code> - This keeps double letters</p>
  
  <p>Currently I remove duplicates by doing:</p>
  
  <pre><code>private String removeDuplicates(String word) {
      StringBuffer buffer = new StringBuffer();
      for (int i = 0; i &lt; word.length(); i++) {
          char letter = word.charAt(i);
          if (buffer.length() == 0 &amp;&amp; letter != buffer.charAt(buffer.length() - 1)) {
              buffer.append(letter);
          }
      }
      return buffer.toString();
  }
  </code></pre>
  
  <p>If I want to keep double letters I was thinking of having a method like private String <code>removeDuplicates(String word, boolean doubleLetter)</code></p>
  
  <p>When <code>doubleLetter</code> is true it will return <code>hello</code> not <code>helo</code></p>
  
  <p>I'm not sure of the most efficient way to do this without duplicating a lot of code.</p>