ivanavitdev
ivanavitdev

Reputation: 2888

Javascript: Remove original and duplicate character from string

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'

Upvotes: 3

Views: 980

Answers (6)

onecompileman
onecompileman

Reputation: 940

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('')

Upvotes: 1

Joven28
Joven28

Reputation: 769

Try this.

var str = "DeadSea";
var s = str.toLowerCase();
var arr = s.split("");
arr.forEach(a => {
    if (arr.indexOf(a) !== arr.lastIndexOf(a)) {
        str = str.replace(new RegExp(a, "gi"), "");
    }
});
console.log(str);

Upvotes: 2

Amadan
Amadan

Reputation: 198324

This should be faster, as there's no indexOf inside a loop:

const removeDuplicateChar = s => {
  let counts = Array.from(s.toLowerCase()).reduce(
    (counts, char) => counts.set(char, (counts.get(char) || 0) + 1) && counts,
    new Map());
  return Array.from(s).filter(letter =>
    counts.get(letter.toLowerCase()) == 1
  ).join('');
}

['services', 'stress', 'ServicEs', 'streSs', 'DeadSea'].forEach(word =>
  console.log(word, removeDuplicateChar(word))
)

Upvotes: 3

Andria
Andria

Reputation: 5075

The magic of .toLowerCase

You just need to use .toLowerCase whenever you deal with checking, but not before because you'll permanently alter the result to all lowerCase, which is not what you want.

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

Granted, this is the messy edition, but it keeps your code so you don't have to change much.

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386550

You could take a lower case string and use lower case letters to find.

function unique(s) {
    var l = s.toLowerCase();
    return Array
        .from(s, c => l.indexOf(c.toLowerCase()) === l.lastIndexOf(c.toLowerCase()) ? c: '')
        .join('');
}

console.log(['services', 'stress', 'ServicEs', 'streSs', 'DeadSea'].map(unique));

Upvotes: 1

Timmetje
Timmetje

Reputation: 7694

Answer as why your code doesn't give the expected output:

indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).

Which makes indexOf() case sensitive.

The solution:

You need to normalize the case of both strings before you do indexOf

You can make a methods like this:

function indexOfCaseInsenstive(a, b) {
  a = a.toLowerCase();
  b = b.toLowerCase();

  return a.indexOf(b);
}

function lastIndexOfCaseInsenstive(a, b) {
  a = a.toLowerCase();
  b = b.toLowerCase();

  return a.lastIndexOf(b);
}

Or use toLowerCase() in your code.

Upvotes: 1

Related Questions