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
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))
)
Comments
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
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
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
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
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
s.toLowerCase() twice for all elements in s, an overhead which could be easily reduced by caching this unchanging value beforehand.