public static String compactRepeatedChars(@NonNull CharSequence sequence) {
      StringBuilder builder = new StringBuilder();
      for (int i = 0; i < sequence.length(); i++) {
         char c = sequence.charAt(i);
         if (builder.length() == 0 || builder.charAt(builder.length() - 1) != c) {
            builder.append(c);
         }
      }
      return builder.toString();
   }
public static String compactRepeatedChars ( @ NonNull CharSequence sequence ) { StringBuilder builder = new StringBuilder ( ) ; for ( int i = 0 ; i < sequence . length ( ) ; i ++ ) { char c = sequence . charAt ( i ) ; if ( builder . length ( ) == 0 || builder . charAt ( builder . length ( ) - 1 ) != c ) { builder . append ( c ) ; } } return builder . toString ( ) ; }
<p>Replaces repeated characters with a single instance. e.g. <code>Gooooood</code> would become
<code>God</code>.</p>

@param sequence The character sequence
@return The compacted string
@throws NullPointerException when the sequence is null