extrema

extrema(itr)

Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple.

Examples

julia> extrema([5, 2, 9, 1, 7])
(1, 9)

In this example, the extrema function is used on an array to compute both the minimum and maximum elements. The function returns a tuple (1, 9) where 1 is the minimum element and 9 is the maximum element.

julia> extrema([10, 20, 30, 40, 50])
(10, 50)

Here, the function is applied to an array with all elements in ascending order. The resulting tuple (10, 50) represents the minimum and maximum values respectively.

julia> extrema([-5, -3, -10, -1])
(-10, -1)

In this case, the extrema function is used on an array of negative numbers. The resulting tuple (-10, -1) represents the minimum and maximum values, respectively.

Common mistake example:

julia> extrema([])
ERROR: MethodError: no method matching extrema(::Array{Any,1})

In this example, an empty array is passed to the extrema function. This results in a MethodError because the function expects a non-empty iterable. Make sure to provide a non-empty collection to the extrema function to avoid this error.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: