3

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'

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

yeah this works great. thanks man. however I might still need a more readable code if possible :)
2

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

1 Comment

Nice use of regex :D
1

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.

Comments

1

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

3 Comments

genius as usual
@SalmanA, stings are immutable, so no it does not change the original string.
@NinaScholz sorry i wasn't thinking straight.
1

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

2 Comments

Nice reuse of the variable from the initial param to make it compact and readable at the same time
Just for the sake of brevity, this function is quite slow. It computes s.toLowerCase() twice for all elements in s, an overhead which could be easily reduced by caching this unchanging value beforehand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.