Here's what I got:
string = 'John bought bought black paint for his black car'
words = string.split(' ')
duplicatelessWords = []
for (const word of words) {
if (duplicatelessWords[duplicatelessWords.length - 1] !== word) {
duplicatelessWords.push(word)
}
}
duplicatelessString = duplicatelessWords.join(' ')
It should return "John bought black paint for his black car"
Is there a way to make it faster? I think I will need to use it many times in a row, sometimes on short strings, sometimes on large.