Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the longest words in the string and return those (excluding duplicates) along with maximum length?

I know how I can find the longest word in a string. For example this code here. But here the problem is that the word "bbbbbb" is found because he is the FIRST LONGEST WORD IN THE string,after that with 6 chars we have also the word "jumped". My question is how can I find in this case and the word "jumped",so all of them not only the first one.

UPDATE: I want unique list, so only one of each words

function longestWord(sentence) {
  sentence = sentence.split(' ');

  let theWord = sentence[0];
  var longest = 0;
  for (let i = 0; i < sentence.length; i++) {
    if (sentence[i] != "") {
      if (sentence[i].length > theWord.length) {
        longest = sentence[i].length;
        theWord = sentence[i];
      }
    }
  }
  return {
    length: longest,
    actuallWord: theWord
  }
}
console.log(longestWord("The quick brown as bbbbbb fox jumped over the bbbbbb lazy dog"));
like image 619
Trajce12 Avatar asked Feb 10 '20 14:02

Trajce12


People also ask

How do you find the longest word in a string python?

Find the length of the longest word using the len() (The number of items in an object is returned by the len() method. It returns the number of characters in a string when the object is a string) and max() (returns the highest-valued item, or the highest-valued item in an iterable) functions from the above words list.

How do you find the longest word in a string C++?

To find the smallest and largest word, we will find the length of each word by using two indexes, one for the start of the word and one for the ending which is marked using the ' ' (space character) or '\0' character. Then using the start and end index, we will find the maxLength and minlength.


3 Answers

function longestWord(sentence) {
  // First we divide the sentence into words
  var words = sentence.split(' ');
  
  // We then get the length by getting the maximun value of the length of each word
  var length = Math.max(...words.map(a=>a.length));
  return {
    length: length,
    // Finally we filter our words array returning only those of with the same length we previously calculated
    words: words.filter(i => i.length == length)
  }
}
console.log(longestWord("The quick brown as bbbbbb fox jumped over the lazy dog"));
like image 159
Luis felipe De jesus Munoz Avatar answered Sep 29 '22 10:09

Luis felipe De jesus Munoz


You could take a single loop approach and check the legth fot each word with the accumulators length of the first item.

function longestWords(words) {
    return words
        .split(/\s+/)
        .reduce((accu, word) => {
            if (!accu[0] || accu[0].length < word.length) return [word];
            if (accu[0].length === word.length) accu.push(word);
            return accu;
        }, []);
}

console.log(longestWords("The quick brown as bbbbbb fox jumped over the lazy dog"));
like image 42
Nina Scholz Avatar answered Sep 29 '22 10:09

Nina Scholz


You may do that with Array.prototype.reduce() by a single pass through the array (without extra looping for calculating maximum length).

The idea is to reset resulting array with a single word, once its length exceeds those that were inserted before or append if current word happens to have same length, or simply pass by otherwise:

const src = 'The quick brown as bbbbbb fox jumped over the jumped lazy dog',

      result = src.split(' ')
                  .reduce((res, word, idx) => {
                    if(!idx || word.length > res.length)
                        res = {length: word.length, words:new Set([word])}
                    else if(word.length == res.length)
                        res.words.add(word)
                    return res
                  }, {})

console.log({result: result.length, words: [...result.words]})
.as-console-wrapper {min-height:100%}
like image 40
Yevgen Gorbunkov Avatar answered Sep 29 '22 09:09

Yevgen Gorbunkov