Code Challenge #1: Removing Duplicate Words from a String

Code Challenge #1: Removing Duplicate Words from a String

Welcome to my new series, Code Challenge!

In this series, I'll be doing challenges that I randomly see on blogs or Discord groups. I enjoy doing them because I learn so much!

I found today's challenge from the Web Developer Bootcamp Discord group.

The Challenge

The task is to remove all duplicate words from a string, leaving only single (first) word entries.

// Example
const str = 'alpha alpha beta beta gamma gamma delta alpha beta beta gamma delta delta';

removeDuplicateWords(str); // 'alpha beta gamma delta';

Stop reading if you want to try this on your own first!

My Solution

I had fun doing this because it let me use methods I don't use often:

Here's my solution:

// My solution
const removeDuplicateWords = string => {
    const arrayOfWords = string.split(" ");

    const arrayOfUniqueWords = arrayOfWords.filter((word, index, array) => {
        return array.indexOf(word) === index;
    });

    const stringOfUniqueWords = arrayOfUniqueWords.join(" ");

    return stringOfUniqueWords;
}

const str = 'alpha alpha beta beta gamma gamma delta alpha beta beta gamma delta delta';

const uniqueWords = removeDuplicateWords(str);
console.log(uniqueWords); // 'alpha beta gamma delta'

Walkthrough

In my solution, I wrote a function called removeDuplicateWords that takes a string.

Split Method

The first line of the function uses the split method for strings:

const arrayOfWords = string.split(" ");
// ["alpha","alpha","beta","beta","gamma","gamma","delta","alpha","beta","beta","gamma","delta","delta"]

The split method takes a string and divides it into substrings based on the given parameter, in this instance a space, and returns it in an array.

Filter and indexOf

Having the string split into substrings in an array let me use the filter method in the next line:

const arrayOfUniqueWords = arrayOfWords.filter((word, index, array) => {
    return array.indexOf(word) === index;
});

// ["alpha", "beta", "gamma", "delta"]

With filter, the callback function uses 3 arguments here:

  • word - the current element being processed
  • index - the index of the current element (optional)
  • array - the arrayOfWords array (optional)

You can read more on how to use the filter method here.

The filter is filtering for words where the index of the first instance of each word (indexOf) is equal to its index.

For example:

// arrayOfWords
["alpha","alpha","beta","beta","gamma","gamma","delta","alpha","beta","beta","gamma","delta","delta"]

Here, the second alpha will be filtered out because the indexOf('alpha') is 0 (index of its first instance) and the second alpha is index 1.

Join Method

Finally, the last few lines wrap it up:

const stringOfUniqueWords = arrayOfUniqueWords.join(" ");
// "alpha beta gamma delta"

return stringOfUniqueWords;

The join method takes an array and breaks it into a string using the given parameter. Basically the opposite of the split method for strings!

Here's the CodePen if you'd like to play around with the code!

It's Your Turn

Did you do this differently? Let me know in the comments!