Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

remove the duplicate character in string js

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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #remove #duplicate #character #string #js
ADD COMMENT
Topic
Name
5+2 =