I wrote a code and it works fine, But is there any easy and more handy way to implement that using javascript RegExp Object or any other way ?
function removeDuplicateChar(str) {
var temp = [], j = 0;
var arr = str.split("");
arr.sort();
for(var i = 0; i < arr.length-1; i++) {
if(arr[i] != arr[i+1]) {
temp[j++] = arr[i];
}
}
temp[j++] = arr[arr.length-1];
for(var i = 0; i < j; i++) {
arr[i] = temp[i];
}
return arr.join("").substring(0,j);
}
console.log(removeDuplicateChar("Rasikawef dfv dd"));
const removeDuplicateChar = s => [...new Set(s)].join("");
- note that i didn't pay too much attention to performance, i just made a short alternative.