最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Javascript: Remove original and duplicate character from string - Stack Overflow

programmeradmin0浏览0评论

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('')
发布评论

评论列表(0)

  1. 暂无评论