In Julia, it's most efficient to use the available functions maximum
and minimum
,
@show maximum([1,5,90,-2,213,2])
> maximum([1, 5, 90, -2, 213, 2]) = 213
@show minimum([1,5,90,-2,213,2])
> minimum([1, 5, 90, -2, 213, 2]) = -2
But sometimes, we need a slightly customized version, so here's how it's done in \(O(n)\).
function mymax(ary)
largest = (nothing, nothing)
for (k, el) in enumerate(ary)
if isnothing(largest[1]) || largest[2] < el
largest = (k, el)
end
end
return largest
end