
which method is used in java to eliminate the duplicate elements in String?

import java.util.*;
class RemoveDuplicates{
public static void main(String[] args){
String[] str = new String[] {
"Where",
"there",
"is",
"will",
"there",
"is",
"a",
"way"};
Arrays.sort(str);
int k = 0;
for (int i = 0; i < str.length; i++){
if (i > 0 && str[i].equals(str[i -1]))
continue;
str[k++] = str[i];
}
String[] st = new String[k];
System.arraycopy(str, 0, st, 0, k);
for (int i = 0; i < st.length; i++)
System.out.println(st[i]);
}
}

import java.util.*;
class RemoveDuplicates{
public static void main(String[] args){
String[] str = new String[] {
"Where",
"there",
"is",
"will",
"there",
"is",
"a",
"way"};
Arrays.sort(str);
int k = 0;
for (int i = 0; i < str.length; i++){
if (i > 0 && str[i].equals(str[i -1]))
continue;
str[k++] = str[i];
}
String[] st = new String[k];
System.arraycopy(str, 0, st, 0, k);
for (int i = 0; i < st.length; i++)
System.out.println(st[i]);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.