Remove Duplicates
String -- Remove Duplicates
Write a return method that can remove the duplicated values from String
Ex: removeDup("AAABBBCCC") ==> ABC
USING CORE JAVA
Public static void main(String[] args){
removeDup(“AAABBBCCC”).sout;
}
public static String removeDup( String str) {
String result = "";
for (int i = 0; i < str.length(); i++)
if (!result.contains("" + str.charAt(i)))
result += "" + str.charAt(i);
return result;
}
USING LINKED HASH SET
Public static void main(String[] args){
removeDup(“AAABBBCCC”).sout;
}
public static String removeDup(String str) {
str = new LinkedHashSet<String>(Arrays.asList(str.split(""))).toString();
str = str.replace(", " , "" ).replace("[","").replace("]","");
return str;
}