The max(by:) approach used in @MartinR:s answer is the fit for purpose solution here, but (as I commonly write :), for the joy of alternatives, you could also "manually" fold over the indices over your array to find the index which corresponds to the longest sub-array (maximum .count):
let arr = [["a", "b", "c"], ["d", "e", "f", "g", "h", "i", "j"]]
if arr.count > 0 {
let longestSubArray = arr[
arr.indices.reduce(0) { arr[$1].count > arr[$0].count ? $1 : $0 } // (*)
]
print(longestSubArray) // ["d", "e", "f", "g", "h", "i", "j"]
}
/* (*) Alternatively:
arr.indices.lazy.map { arr[$0].count }.max() ?? 0 */
In case you'd like to find the subset of subarrays that have the largest count (which may not be a single one), you could use a similar approach combined with filter:
// two longest subarrays with same count
let arr = [["a", "b", "c"],
["d", "e", "f", "g", "h", "i", "j"],
["k", "l", "m", "n", "o", "p", "q"]]
let maxSubArrayCount = arr.lazy.map { $0.count }.max() // thanks @muescha (+)
let longestSubArrays = arr.filter { $0.count == maxSubArrayCount }
print(longestSubArrays) /* [["d", "e", "f", "g", "h", "i", "j"],
["k", "l", "m", "n", "o", "p", "q"]] */
/* (+) Alternatively:
let maxSubArrayCount = arr.reduce(0) { $1.count > $0 ? $1.count : $0 } */