Advertisement
sweet1cris

Untitled

Sep 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. public class RemoveDuplicateI {
  2.   // Try to convert the input to char[],
  3.   // and do it in place.
  4.   public String deDup(String input) {
  5.     if (input == null) {
  6.       return null;
  7.     }
  8.     char[] array = input.toCharArray();
  9.     int end = 0;
  10.     for (int i = 0; i < array.length; i++) {
  11.       // repeated characters will be ignored except
  12.       // for the first character in the sequence.
  13.       if (i == 0 || array[i] != array[end - 1]) {
  14.         array[end++] = array[i];
  15.       }
  16.     }
  17.     return new String(array, 0, end);
  18.   }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement