Can anyone please let me know how to remove duplicate values from
String s=\"Bangalore-Chennai-NewYork-Bangalore-Chennai\";
and output sh
import java.util.HashSet;
public class SplitString {
public static void main(String[] args) {
String st = new String("New Delhi-Chennai-New York-Bangalore-Chennai-New Delhi-Chennai-New York");
StringBuffer stb = new StringBuffer();
HashSet<String> hashset = new HashSet<String>();
for (String a : st.split("-"))
hashset.add(a);
Object[] str = (Object[]) hashset.toArray();
for (int i = 0; i < str.length; i++) {
stb.append(str[i]);
if (i < str.length - 1)
stb.append("-");
}
System.out.println(stb);
}
}
This does it in one line:
public String deDup(String s) {
return new LinkedHashSet<String>(Arrays.asList(s.split("-"))).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", "-");
}
public static void main(String[] args) {
System.out.println(deDup("Bangalore-Chennai-NewYork-Bangalore-Chennai"));
}
Output:
Bangalore-Chennai-NewYork
Notice that the order is preserved :)
Key points are:
split("-")
gives us the different values as an arrayArrays.asList()
turns the array into a ListLinkedHashSet
preserves uniqueness and insertion order - it does all the work of giving us the unique values, which are passed via the constructortoString()
of a List is [element1, element2, ...]
replace
commands remove the "punctuation" from the toString()
This solution requires the values to not contain the character sequence ", "
- a reasonable requirement for such terse code.
Of course it's 1 line:
public String deDup(String s) {
return Arrays.stream(s.split("-")).distinct().collect(Collectors.joining("-"));
}
If you don't care about preserving order (ie it's OK to delete the first occurrence of a duplicate):
public String deDup(String s) {
return s.replaceAll("(\\b\\w+\\b)-(?=.*\\b\\1\\b)", "");
}
I'd prefer this which is simpler than all of the above.
public void removeDuplicates() {
String myString = "Bangalore-Chennai-NewYork-Bangalore-Chennai";
String[] array = myString.split("-");
Set<String> hashSet = new HashSet<String>(Arrays.asList(array));
String newString = StringUtils.join(hashSet, "-");
}
public static String removeDuplicates(String txt, String splitterRegex)
{
List<String> values = new ArrayList<String>();
String[] splitted = txt.split(splitterRegex);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitted.length; ++i)
{
if (!values.contains(splitted[i]))
{
values.add(splitted[i]);
sb.append('-');
sb.append(splitted[i]);
}
}
return sb.substring(1);
}
Usage:
String s = "Bangalore-Chennai-NewYork-Bangalore-Chennai";
s = removeDuplicates(s, "\\-");
System.out.println(s);
Prints:
Bangalore-Chennai-NewYork
StringBuilder builderWord = new StringBuilder(word);
for(int index=0; index < builderWord.length(); index++) {
for(int reverseIndex=builderWord.length()-1; reverseIndex > index;reverseIndex--) {
if (builderWord.charAt(reverseIndex) == builderWord.charAt(index)) {
builderWord.deleteCharAt(reverseIndex);
}
}
}
return builderWord.toString();
static String RemoveDuplicateCharInString(String s){
for (int i = 0; i < s.length(); i++) {
if((s.substring(i+1)).indexOf(s.charAt(i))!=-1){
s=s.substring(0,i+1)+(s.substring(i+1)).replaceAll(""+s.charAt(i),"");
}
}
return s;
}