DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Updated on

Algorithm 101: 6 Ways to Find the Longest Word in a Sentence

This episode of Algorithm 101 features finding the longest word. In how many ways can you do this?

We will be looking at 6 ways to Find the Longest Word in a Sentence or group of words.


longestWord("Njoku Samson Ebere"); // Samson

longestWord("Find the longest word"); // longest

Enter fullscreen mode Exit fullscreen mode

Prerequisite

This article assumes that you have basic understanding of javascript's string and array methods.

Let's Find the Longest Word in a Sentence using:

  • .split() and .sort()
  function longestWord(sentence) {
    return sentence.split(" ").sort((word, nextWord) => nextWord.length - word.length)[0];
  }
Enter fullscreen mode Exit fullscreen mode
  • forEach(), if...statement and .split()
      function longestWord(sentence) {
        let splittedSentence = sentence.split(" ");
        let maxLength = 0;
        let maxWord = "";

        splittedSentence.forEach(word => {
          if (word.length > maxLength) {
            maxLength = word.length;
            maxWord = word;
          }
        });

        return maxWord;
      }
Enter fullscreen mode Exit fullscreen mode
  • map(), if...statement and .split()
      function longestWord(sentence) {
        let splittedSentence = sentence.split(" ");
        let maxLength = 0;
        let maxWord = "";

        splittedSentence.map(word => {
          if (word.length > maxLength) {
            maxLength = word.length;
            maxWord = word;
          }
        });

        return maxWord;
      }
Enter fullscreen mode Exit fullscreen mode
  • for...loop, if...statement and .split()
      function longestWord(sentence) {
        let splittedSentence = sentence.split(" ");
        let maxLength = 0;
        let maxWord = "";

        for (let i = 0; i < splittedSentence.length; i++) {
          if (splittedSentence[i].length > maxLength) {
            maxLength = splittedSentence[i].length;
            maxWord = splittedSentence[i];
          }
        }

        return maxWord;
      }
Enter fullscreen mode Exit fullscreen mode
  • for...of...loop, if...statement and .split()
      function longestWord(sentence) {
        let splittedSentence = sentence.split(" ");
        let maxLength = 0;
        let maxWord = "";

        for (word of splittedSentence) {
          if (word.length > maxLength) {
            maxLength = word.length;
            maxWord = word;
          }
        }

        return maxWord;
      }
Enter fullscreen mode Exit fullscreen mode
  • for...in...loop, if...statement and .split()
      function longestWord(sentence) {
        let splittedSentence = sentence.split(" ");
        let maxLength = 0;
        let maxWord = "";

        for (word in splittedSentence) {
          if (splittedSentence[word].length > maxLength) {
            maxLength = splittedSentence[word].length;
            maxWord = splittedSentence[word];
          }
        }

        return maxWord;
      }
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. You are only limited by your imagination. This could also be achieved using .reduce() method. Why not try it out and tell us how you did it in the comment section?

I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

Up Next: Algorithm 101: 9 Ways to Search and Replace a Word

You can also follow and message me on social media platforms.

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (8)

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan • Edited

Here's the reduce version for those who are wondering:

function longestWord(sentence) {
    return sentence.split(" ").reduce((longestWord, currentWord) => {
        return currentWord.length > longestWord.length ? currentWord : longestWord;
    }, '');
}

Edited, to clarify how the code works:

  1. sentence.split(" ") returns an array of words.
  2. Since the previous step creates an array, we can call reduce on it.
  3. Reduce takes two arguments: the "accumulator" (in this case, the longest word) and the current element (currentWord).
  4. In each iteration of the callback, we return the longer of the two words.
  5. Reduce initially starts with the empty string, to guarantee that there's always something longer than it.
Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

This is awesome, Aleksandr. Works perfectly. Can you walk us through the code?

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

Sure, edited to clarify

Thread Thread
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you for the explanation. You are good.

Collapse
 
pamprog profile image
PamProg

Hey !
I don't think you should use map like this. You use it like a forEach, and junior developer could think it's okay to use it like this while it's not :/

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

PamProg thank you for taking your time to read and comment. Can you explain further?

Collapse
 
pamprog profile image
PamProg

Well, your "forEach" and "map" examples are the same, so we could think that both are good to use. But the map (and I'm sure you know it) is used to make a new array, not to find something, filter something or whatever.
It's not because you can do it with map that you should use it and promote it :)

Thread Thread
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thank you. You are awesome 😍