For example
String str="**Hello bye bye given given world world**"
should return
"**Hello bye given world**".
For example
String str="**Hello bye bye given given world world**"
should return
"**Hello bye given world**".
You could split the string by using space and and compare the string with previous string occurrences by storing the non duplicate values only in a list.
The code will be as follows.
import java.util.ArrayList;
import java.util.List;
public class Test2 {
private static final String SPACE = " ";
public static void main(String[] args) {
System.out.println(replaceDuplicateString("**Hello Bye bye given given world world**"));
}
public static String replaceDuplicateString(String input) {
List<String> addedList = new ArrayList<>();
StringBuilder output = new StringBuilder();
for (String str: input.split(SPACE)) {
if (!addedList.contains(str.toUpperCase())) {
output.append(str);
output.append(' ');
addedList.add(str.toUpperCase());
}
}
return output.toString().trim();
}
}
This will print
Hello bye given world
How ever if you change the input to
**Hello bye bye given given world world**
the output will be
**Hello bye given world world**
Since, world
is not a duplicate of world**
.