0

I am trying to find all the longest strings in an array.

For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
allLongestStrings(inputArray) = ["aba", "vcd", "aba"].

So far I have the code

function allLongestStrings(inputArray) {
    let longboys = []
 for (i = 0; i < inputArray.length; i++) {
 if (inputArray[i].length) {
     // what to do here    
     longboys.push()
 }
}
return longboys 
}

I am stumped on how to find the longest string and then use the length of that string to add others to the array.
Sorry if this has already been answered elsewhere, I've been finding lots of "find the longest string in an array" but nothing on finding multiple.

imstupidpleasehelp
  • 1,618
  • 2
  • 16
  • 36
  • 1
    `let longest = Math.max(...inputArray.map(s => s.length))` then `return inputArray.filter(s => s.length === longest)` – Bravo Nov 27 '20 at 04:50

5 Answers5

2

method

  1. Find the longest string
  2. Filter array for those strings with that length

function allLongestStrings(inputArray) {
  let longest = Math.max(...inputArray.map(({length}) => length));
  return inputArray.filter(({length}) => length === longest);
}
console.log(allLongestStrings(["aba", "aa", "ad", "vcd", "aba"]));
Bravo
  • 6,022
  • 1
  • 10
  • 15
0

I'd make an object whose keys are the lengths of the strings in the values: eg { 2: ['aa', 'ad'] } - then at the end, find the largest number in the keys, and return the array there:

const allLongestStrings = (inputArray) => {
  const strsByLength = {};
  for (const str of inputArray) {
    const { length } = str;
    if (!strsByLength[length]) strsByLength[length] = [];
    strsByLength[length].push(str);
  }
  return strsByLength[Math.max(...Object.keys(strsByLength))];
};

console.log(allLongestStrings(["aba", "aa", "ad", "vcd", "aba"]));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

Try this :

 function allLongestStrings(inputArray) {
  const longboys = [];
  let maxLength;
  for (let i = 0,j = 1; i < inputArray.length - 1, j < inputArray.length; i++ , j++ ) {
    if (inputArray[i].length < inputArray[j].length) {
      maxLength = inputArray[i].length;
    } else {
      maxLength = inputArray[j].length;
    }
  }
  for (let i = 0; i < inputArray.length; i++) {
    if (inputArray[i].length === maxLength) {
      longboys.push(inputArray[i]);
    }
  }
  return longboys;
}
Shariq
  • 175
  • 6
0

The previous versions look too fiddly to me and require two passes over the data. How about just:

function allLongestStrings(inputArray) {
    let longboys = [];
    let max_length = 0;
    for (const str of inputArray) {
        if (str.length > max_length) {
            longboys = [];
            max_length = str.length
            longboys.push(str);
        }
        else if (str.length == max_length) {
            longboys.push(str);
        }
    }
    return longboys 
};

console.log(allLongestStrings(["bb", "aba", "aa", "ad", "vcd", "aba"]));

We record all the equally long strings we've found as we go down the list, updating the maximum length as we go.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
0
function allLongestStrings(inputArray) {    
    let longboys = []
    var sortedArray = inputArray.sort(function(a, b){
        return b.length - a.length;
    });
    var maxLength = sortedArray[0].length;
    for(i=0;i<sortedArray.length;i++){
        if(sortedArray[i].length == maxLength) {
            longboys.push(sortedArray[i]);
        }
        else {
            break;
        }
    }
    return longboys;
}
ishak
  • 19
  • 2