I need help with a scripting-challenge (easy)

Hello! I’m doing some scripting challenges and I came across this:


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] 3 seconds (lua)
  • [input] array.string inputArray A non-empty array. Guaranteed constraints:
    1 ≤ inputArray.length ≤ 10 ,
    1 ≤ inputArray[i].length ≤ 10 .
  • [output] array.string Array of the longest strings, stored in the same order as in the inputArray .

Basically it should return the longest strings, not only one.
I’ve made something like that rq but it only returns one of the longest strings.

function allLongestStrings(inputArray)
    local firstLongestString
    for i,v in next, inputArray do
        if firstLongestString == nil or #v > #firstLongestString then
            firstLongestString = v
        end
    end
    return firstLongestString
end

If you have a (simple) solution for this, please also explain your code.
Thanks!

Saw this solution from someone else, thanks to him

function allLongestStrings(inputArray)
    local longestLen = 0
    local returnTab = {}
    
    for i, v in ipairs(inputArray) do
        if string.len(v) > longestLen then
            longestLen = string.len(v)
        end
    end
    
    for i, v in ipairs(inputArray) do
        if string.len(v) == longestLen then
            table.insert(returnTab, v)
        end
    end
    
    return returnTab
end