How to remove non-duplicate characters from string in TypeScript

1 Answer

0 votes
const removeNonDuplicate = (str: string): string => {
   const arr: string[] = str.split("");

   const duplicateArray: string[] = arr.filter(ch => {
        return arr.indexOf(ch) !== arr.lastIndexOf(ch);
   });

   return duplicateArray.join("");
}

const str: string = "Bubble Occurrence Mammal type";

console.log(removeNonDuplicate(str));


 
 
 
/*
run:

"ubble ccurrece ammal e" 
 
*/

 



answered Mar 20, 2024 by avibootz
...