min-max-array/main.go

32 lines
456 B
Go

package minmax
// Returns either the largest value or the smallest value of an array
// TODO: Handle empty arrays without panicking
func Min(arr []float64) (min float64) {
if len(arr) == 0 {
return 0
}
min = arr[0]
for _, v := range arr {
if v < min {
min = v
}
}
return min
}
func Max(arr []float64) (max float64) {
if len(arr) == 0 {
return 0
}
max = arr[0]
for _, v := range arr {
if v > max {
max = v
}
}
return max
}