Javascript: Remove original and duplicate character from string

  • A+

What I need is the remove both original character and its duplicates regardless if its lowercase or uppercase. also it should retain if its uppercase or lowercase

Here is my current code but it can't filter if the string is uppercase then lowercase.

const removeDuplicateChar = s => s   .split('')   .filter( ( cur, index, self ) => self.lastIndexOf( cur ) === self.indexOf( cur ) )   .join('') 

Actual Output

'services' becomes 'rvic' 'stress' becomes 'tre' 'ServicEs' becomes 'ServicEs' 'streSs' becomes 'treS' 'DeadSea' becomes 'DdS' 

Expected Output

'services' should be 'rvic' 'stress' should be 'tre' 'ServicEs' should also be 'rvic' 'streSs' should also be 'tre' 'DeadSea' becomes 'S' 


You need to compare the lastIndexOf and indexOf of the character's instance and use s as reference so that, you won't need to use self and join it to be a string again.

const filterDuplicateCharacters = s => s   .split('')   .filter((c) => s.toLowerCase().lastIndexOf(c.toLowerCase()) ===               s.toLowerCase().indexOf(c.toLowerCase()))   .join('') 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: