Added my simpler implementation for the recursive max function (#300)

This commit is contained in:
RyanM-Dev
2025-05-02 03:02:08 +03:30
committed by GitHub
parent ceea225d2e
commit b3dbf79d05

View File

@@ -2,21 +2,18 @@ package main
import "fmt"
func max(list []int) int {
if len(list) == 2 {
if list[0] > list[1] {
return list[0]
}
return list[1]
}
subMax := max(list[1:])
if list[0] > subMax {
return list[0]
}
return subMax
}
func main() {
fmt.Println(max([]int{1, 5, 10, 25, 16, 1}))
}
func max(arr []int) int {
if len(arr) == 1 {
return arr[0]
}
if arr[0] > max(arr[1:]) {
return arr[0]
}
return max(arr[1:])
}