Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicate same characters in a row

Tags:

java

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.

For example:

helllllllo -> helo

or

helllllllo -> hello - This keeps double letters

Currently I remove duplicates by doing:

private String removeDuplicates(String word) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < word.length(); i++) {
        char letter = word.charAt(i);
        if (buffer.length() == 0 && letter != buffer.charAt(buffer.length() - 1)) {
            buffer.append(letter);
        }
    }
    return buffer.toString();
}

If I want to keep double letters I was thinking of having a method like private String removeDuplicates(String word, boolean doubleLetter)

When doubleLetter is true it will return hello not helo

I'm not sure of the most efficient way to do this without duplicating a lot of code.

like image 703
Decrypter Avatar asked Dec 01 '25 06:12

Decrypter


2 Answers

why not just use a regex?

 public class RemoveDuplicates {
      public static void main(String[] args) {
           System.out.println(new RemoveDuplicates().result("hellllo", false)); //helo
           System.out.println(new RemoveDuplicates().result("hellllo", true)); //hello
      }

      public String result(String input, boolean doubleLetter){
           String pattern = null;
           if(doubleLetter) pattern = "(.)(?=\\1{2})";
           else pattern = "(.)(?=\\1)";
       return input.replaceAll(pattern, "");
      }
 }

 (.)    --> matches any character and puts in group 1. 
 ?=     --> this is called a positive lookahead. 
 ?=\\1  --> positive lookahead for the first group

So overall, this regex looks for any character that is followed (positive lookahead) by itself. For example aa or bb, etc. It is important to note that only the first character is part of the match actually, so in the word 'hello', only the first l is matched (the part (?=\1) is NOT PART of the match). So the first l is replaced by an empty String and we are left with helo, which does not match the regex

The second pattern is the same thing, but this time we look ahead for TWO occurrences of the first group, for example helllo. On the other hand 'hello' will not be matched.

Look here for a lot more: Regex

P.S. Fill free to accept the answer if it helped.

like image 167
Eugene Avatar answered Dec 03 '25 22:12

Eugene


try

    String s = "helllllllo";
    System.out.println(s.replaceAll("(\\w)\\1+", "$1"));

output

helo
like image 40
Evgeniy Dorofeev Avatar answered Dec 03 '25 20:12

Evgeniy Dorofeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!