Remove duplicates from string without using additional buffer I wrote a simple program to remove duplicates from a String without using additional buffer.

The program should filter the duplicates and return just the unique string.

Example:


  Input:
  FOOOOOOOOLLLLLOWWWWWWWWWW UUUUP
  
  Output:
  FOLW UP


I just want to know if the below solution is a good solution for my problem statement.

public class compareString {
    public static void main(String args[]) {
        removeDuplicateString("FOOOOOOOOLLLLLOWWWWWWWWWW UUUUP");
    }

    public static void removeDuplicateString(String input) {
        String value1 = input;
        String value2 = input;
        String finalValue = "";
        int count = 0;
        char char1;
        char char2 = 0;
        for (int i = 0; i &lt; value1.length(); i++) {
            flag = 0;
            char1 = value1.charAt(i);
            for (int j = 0; j &lt; value2.length(); j++) {
                char2 = value2.charAt(j);
                if (char1 == char2) {
                    count++;
                }
            }

            if (count &gt; 1) {
                finalValue=finalValue+char1;
                i=i+(count-1);
            } else {
                finalValue = finalValue + char1;
            }
            count = 0;
        }
        System.out.println(finalValue);
    }
}
Thats not working. Try the input: abaa (result: aa)

I do not know what you mean with "without using additional buffer", because finalValue is at least an additional buffer (which is kind of necessary. You could create a view, but this will be a bit complex).

An easier approach would be to create a LinkedHastSet. Insert all characters, output the set, done:

public static void removeDuplicateString2(final String input) {
    final Set&lt;Character&gt; set = new LinkedHashSet&lt;&gt;();
    for (int i = 0; i &lt; input.length(); i++)
        set.add(input.charAt(i));
    final StringBuilder stringBuilder = new StringBuilder(set.size());
    for (final Character character : set)
        stringBuilder.append(character);
    System.out.println(stringBuilder);
}




About your code:

removeDuplicateString


I would call it something like removeMultipleOccurrence



String finalValue = "";


You may use a StringBuilder to reduce string concatenations.



    String value1 = input;
    String value2 = input;


You do not need these variables, just use input instead of value1/2



    int count = 0;
    char char1;
    char char2 = 0;


You do not need char2, and you can put the rest inside the loop to the initializing place.



        flag = 0;


This variable is not defined. 



        if (count &gt; 1) {
            finalValue=finalValue+char1;
            i=i+(count-1);
        } else {
            finalValue = finalValue + char1;
        }


You may move the finalValue line from the if/else. Then you can skip the else part, because it is empty.



If you combine everything, you could have:

public static void removeMultipleOccurrence(final String input) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i &lt; input.length(); i++) {
        int count = 0;
        final char char1 = input.charAt(i);
        for (int j = 0; j &lt; input.length(); j++) {
            if (char1 == input.charAt(j)) {
                count++;
            }
        }

        if (count &gt; 1)
            i = i + (count - 1);
        result.append(char1);
    }
    System.out.println(result);
}


Hint: This is still not working, just doing the same in a more clear way.
If you want to do it in your way, you have to search the string before the current position if you have already the current char.

This could be done in this way:

public static void removeMultipleOccurrence(final String input) {
    String finalValue = "";
    for (int i = 0; i &lt; input.length(); i++) {
        int count = 0;
        final char currentChar = input.charAt(i);
        for (int j = 0; j &lt; i; j++) {
            if (currentChar == input.charAt(j)) {
                ++count;
            }
        }

        if (!(count &gt; 0))
            finalValue = finalValue + currentChar;
    }
    System.out.println(finalValue);
}


If we look at the count, it is just a boolean, so we end up with:

public static void removeMultipleOccurrence(final String input) {
    final StringBuilder result = new StringBuilder();
    for (int i = 0; i &lt; input.length(); i++) {
        boolean alreadySeen = false;
        final char currentChar = input.charAt(i);
        for (int j = 0; j &lt; i; j++) {
            if (currentChar == input.charAt(j)) {
                alreadySeen = true;
                break;
            }
        }

        if (!alreadySeen)
            result.append(currentChar);
    }
    System.out.println(result);
}


We could get rid of the boolean now:

public static void removeMultipleOccurrence(final String input) {
    final StringBuilder result = new StringBuilder();
    withNextChar: for (int i = 0; i &lt; input.length(); i++) {
        final char currentChar = input.charAt(i);
        for (int j = 0; j &lt; i; j++) {
            if (currentChar == input.charAt(j))
                continue withNextChar;
        }
        result.append(currentChar);
    }
    System.out.println(result);
}


Or, we could look in the result, if we have it already:

public static void removeMultipleOccurrence(final String input) {
    final StringBuilder result = new StringBuilder();
    for (int i = 0; i &lt; input.length(); i++) {
        String currentChar = input.substring(i, i + 1);
        if (result.indexOf(currentChar) &lt; 0) //if not contained
            result.append(currentChar);
    }
    System.out.println(result);
}