All Longest Strings

Description


Given an array of strings, return another array containing all of its longest strings.

Example

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

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] array.string inputArray

    A non-empty array.

    Guaranteed constraints:
    5 ≤ inputArray.length ≤ 10,
    0 ≤ inputArray[i] ≤ 10.

  • [output] array.string

    • Array of the longest strings, stored in the same order as in the inputArray.

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}

Solution


1
2
3
4
function allLongestStrings(inputArray) {
  var maxLength = Math.max(...inputArray.map(s => s.length));
  return inputArray.filter(s => s.length === maxLength);
}