function removeDuplicate(str, letSingleChar = false) {
let string = str;
for (element of string) {
const re = new RegExp(element, 'g');
if (string.match(re)?.length > 1) {
if (letSingleChar == true) {
string = [...new Set(string.split(''))].join('');
} else
string = string.replace(re, '');
}
}
return string;
}
console.log(removeDuplicate("Hello World", true)) // Helo Wrd
console.log(removeDuplicate("Hello World", false )) // He Wrd