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
solution(inputArray) = ["aba", "vcd", "aba"].
A non-empty array.
Guaranteed constraints:
1 ≤ inputArray.length ≤ 10,
1 ≤ inputArray[i].length ≤ 10.
Array of the longest strings, stored in the same order as in the inputArray.
I know that the javascript code answer of the above question but did’t know how to write it into dart.
this is the javascript //
var maxLength = Math.max(...inputArray.map(s => s.length));
return inputArray.filter(s => s.length === maxLength);
2
Answers
You can
sort
the list by its item’s length and then search for items that have equal length to the longest item in sorted list. like this:or use
reduce
instead ofsort
, like this:This is pretty easy.
Output: