4
\$\begingroup\$

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 < value1.length(); i++) {
            flag = 0;
            char1 = value1.charAt(i);
            for (int j = 0; j < value2.length(); j++) {
                char2 = value2.charAt(j);
                if (char1 == char2) {
                    count++;
                }
            }

            if (count > 1) {
                finalValue=finalValue+char1;
                i=i+(count-1);
            } else {
                finalValue = finalValue + char1;
            }
            count = 0;
        }
        System.out.println(finalValue);
    }
}
\$\endgroup\$
11
\$\begingroup\$

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<Character> set = new LinkedHashSet<>();
    for (int i = 0; i < 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 > 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 < input.length(); i++) {
        int count = 0;
        final char char1 = input.charAt(i);
        for (int j = 0; j < input.length(); j++) {
            if (char1 == input.charAt(j)) {
                count++;
            }
        }

        if (count > 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 < input.length(); i++) {
        int count = 0;
        final char currentChar = input.charAt(i);
        for (int j = 0; j < i; j++) {
            if (currentChar == input.charAt(j)) {
                ++count;
            }
        }

        if (!(count > 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 < input.length(); i++) {
        boolean alreadySeen = false;
        final char currentChar = input.charAt(i);
        for (int j = 0; j < 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 < input.length(); i++) {
        final char currentChar = input.charAt(i);
        for (int j = 0; j < 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 < input.length(); i++) {
        String currentChar = input.substring(i, i + 1);
        if (result.indexOf(currentChar) < 0) //if not contained
            result.append(currentChar);
    }
    System.out.println(result);
}
\$\endgroup\$

protected by Jamal Apr 3 '14 at 1:51

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.