Answers for "remove duplicate"

0

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;
}
Posted by: Guest on September-29-2021
-1

remove duplicate

let ageGroup = [18, 21, 1, 1, 51, 18, 21, 5, 18, 7, 10];
let uniqueAgeGroup = ageGroup.reduce(function (accumulator, currentValue) {
  if (accumulator.indexOf(currentValue) === -1) {
    accumulator.push(currentValue);
  }
  return accumulator;
}, []);

console.log(uniqueAgeGroup); // [ 18, 21, 1, 51, 5, 7, 10 ]
Posted by: Guest on June-25-2021

Browse Popular Code Answers by Language