Smallest and largest
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Smallest and largest
Find both ends of a list in a single pass.
Keep two values at once, both starting as the first item. Check each number against both: it may replace the smallest, the largest, or neither.
Write min_max(nums) that returns a list [smallest, largest] using one loop and no min or max. The list is never empty.
Click Run to see the output here.