admin管理员组

文章数量:1389754

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' bees 'rvic'
'stress' bees 'tre'
'ServicEs' bees 'ServicEs'
'streSs' bees 'treS'
'DeadSea' bees 'DdS'

Expected Output

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

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' bees 'rvic'
'stress' bees 'tre'
'ServicEs' bees 'ServicEs'
'streSs' bees 'treS'
'DeadSea' bees 'DdS'

Expected Output

'services' should be 'rvic'
'stress' should be 'tre'
'ServicEs' should also be 'rvic'
'streSs' should also be 'tre'
'DeadSea' bees 'S'
Share Improve this question edited Jul 4, 2018 at 10:12 ivanavitdev asked Jul 4, 2018 at 10:07 ivanavitdevivanavitdev 2,9283 gold badges13 silver badges24 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

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

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.

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);

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

indexOf() pares 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.

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));

You need to pare 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('')

本文标签: arraysJavascript Remove original and duplicate character from stringStack Overflow