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);
}