2

I started to read the famous "cracking the Coding Interview" book.

Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.

I found a similar topic here : Remove the duplicate characters in a string

The solution given by the author was that :

  public static void removeDuplicates(char[] str) {
  if (str == null) return;
  int len = str.length;
  if (len < 2) return;

  int tail = 1;

  for (int i = 1; i < len; ++i) {
       int j;

       for (j = 0; j < tail; ++j) {
       if (str[i] == str[j]) break;
       }

       if (j == tail) {
       str[tail] = str[i];
       ++tail;
     }
  }
  str[tail] = 0;
 }

The problem here is that the author used an array to be an argument for this function. So my question is : how can you write an algorithms with a STRING as an argument? Because I felt like it's really easier to use an array here and it's like that you "avoid the difficulty" of the exercice (in my opinion, I'm a newly Java developer).

How can you write such an algorithm?

1
  • For question's sake, let's assume strings were mutable, say you could remove characters from them: You can use String.charAt(position) to get the character on that position the same as you use array[position]. Commented Apr 24, 2016 at 23:39

3 Answers 3

3

Java strings are immutable, so you can't do it with a string without copying the array into a buffer.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding to this ... str[tail] = str[i] is the statement which require an array.
0

for this to work with a String you'd have to return a String from the method that represents the modified str with no duplicates. not sure if it'll go against the rules, but here's how I'd solve the problem with String's:

for each character in the string, i would split the string at that character. i would remove all instances of that character from the latter substring. i would then concatenate the former substring with the modified latter substring, making sure that the character is still kept in it's place. something like this:

public static String removeDuplicates( String str ) {
    if( str == null || str.length() < 2 )
        return str;
    String temp;
    for( int x = 0; x + 1 < str.length(); x++ ) {
        temp = str.charAt( x ) + ""; 
        str = str.substring( 0, x ) + temp + str.substring( x + 1 ).replaceAll( temp, "" );
    }
    return str;
}

Comments

0

In Java 8 we can do it like this

private void removeduplicatecharactersfromstring() {
    String myString = "aabcd eeffff ghjkjkl";
    StringBuilder builder = new StringBuilder();
    System.out.println(myString);
    Arrays.asList(myString.split(" "))
            .forEach(s -> {
                builder.append(Stream.of(s.split(""))
                        .distinct().collect(Collectors.joining()).concat(" "));
            });
    System.out.println(builder); // abcd ef ghjkl
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.