Find the Longest Word in a Sentence

medium

By - Aman Pareek

Last Updated - 28/08/2024

Problem Statement

Write a function that finds and returns the longest word in a given sentence. If there are multiple words with the maximum length, return the first one.

Example 1

Input: str = "Hey its me aman pareek"

Output: "pareek"

Example 2

Input: num = 874156

Output: Throw error => Your input is not a string

Solution 1: Using split, map, and sort

function findLongestWordUsingSplitMapSort(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  return sentence
    .split(" ")
    .map(word => ({ length: word.length, word }))
    .sort((a, b) => b.length - a.length)[0].word;
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitMapSort(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingSplitMapSort(num2);  //Throw error => Your input is not a string 

Solution 2: Using split, map, and filter

function findLongestWordUsingSplitMapFilter(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  const words = sentence.split(" ");
  const maxLength = Math.max(...words.map(word => word.length));
  return words.filter(word => word.length === maxLength)[0];
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitMapFilter(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingSplitMapFilter(num2);  //Throw error => Your input is not a string 

Solution 3: Using split and sort

function findLongestWordUsingSplitAndSort(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  return sentence.split(" ").sort((a, b) => b.length - a.length)[0];
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitAndSort(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingSplitAndSort(num2);  //Throw error => Your input is not a string 

Solution 4: Using a for Loop

function findLongestWordUsingForLoop(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  const words = sentence.split(" ");
  let longestWord = "";
  for (const word of words) {
    if (word.length > longestWord.length) {
      longestWord = word;
    }
  }
  return longestWord;
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingForLoop(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingForLoop(num2);  //Throw error => Your input is not a string 

Solution 5: Using Array.prototype.reduce

function findLongestWordUsingReduce(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  return sentence.split(" ").reduce((longest, word) => 
    word.length > longest.length ? word : longest, ""
  );
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingReduce(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingReduce(num2);  //Throw error => Your input is not a string 

Solution 6: Using Array.prototype.every

function findLongestWordUsingEvery(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  const words = sentence.split(" ");
  let longestWord = words[0];
  words.every(word => {
    if (word.length > longestWord.length) {
      longestWord = word;
    }
    return true; // Continue iteration
  });
  return longestWord;
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingEvery(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingEvery(num2);  //Throw error => Your input is not a string 

Solution 7: Using for...of Loop

function findLongestWordUsingForOf(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  const words = sentence.split(" ");
  let longestWord = "";
  for (const word of words) {
    if (word.length > longestWord.length) {
      longestWord = word;
    }
  }
  return longestWord;
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingForOf(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingForOf(num2);  //Throw error => Your input is not a string 

Solution 8: Using String.prototype.match

function findLongestWordUsingMatch(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  const words = sentence.match(/\w+/g) || [];
  return words.reduce((longest, word) => 
    word.length > longest.length ? word : longest, ""
  );
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingMatch(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingMatch(num2);  //Throw error => Your input is not a string 

Solution 9: Using String.prototype.split and filter

function findLongestWordUsingSplitFilter(sentence) {
  if (typeof sentence !== "string") {
    throw new Error("Your input is not a string");
  }
  const words = sentence.split(" ");
  const maxLength = words.reduce((max, word) => Math.max(max, word.length), 0);
  return words.filter((word) => word.length === maxLength)[0];
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingSplitFilter(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingSplitFilter(num2);  //Throw error => Your input is not a string 

Solution 10: Using Array.prototype.find

function findLongestWordUsingFind(sentence) {
  if (typeof sentence !== 'string') {
    throw new Error("Your input is not a string");
  }
  const words = sentence.split(" ");
  const maxLength = Math.max(...words.map(word => word.length));
  return words.find(word => word.length === maxLength);
} 

const str1 = "Hey its me aman pareek";
findLongestWordUsingFind(str1);  //output: pareek 

const num2 = 874156;
findLongestWordUsingFind(num2);  //Throw error => Your input is not a string 

Popular Solutions