This can be easily achieved by using regex and String.prototype.replace()
.
let wordsList = [
'hheellloo',
'hhooww',
'aaaarrrreee',
'yyyyyoooouuuuu',
];
const removeDuplicateLetters = (word) => {
return word.replace(/(.)\1+/g, (match, contents) => {
return contents.indexOf(match) + 1 ? '' : match;
});
}
wordsList = wordsList.map(word => removeDuplicateLetters(word));
console.log(wordsList);
// ["helo", "how", "are", "you"]
You can reduce the code to only one line.
let wordsList = [
'hheellloo',
'hhooww',
'aaaarrrreee',
'yyyyyoooouuuuu',
];
wordsList = wordsList.map(word => word.replace(/(.)\1+/g, '$1'));
console.log(wordsList);
// ["helo", "how", "are", "you"]