Skip to content

Commit b3dbf79

Browse files
authored
Added my simpler implementation for the recursive max function (#300)
1 parent ceea225 commit b3dbf79

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

04_quicksort/golang/04_recursive_max.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@ package main
22

33
import "fmt"
44

5-
func max(list []int) int {
6-
if len(list) == 2 {
7-
if list[0] > list[1] {
8-
return list[0]
9-
}
10-
return list[1]
11-
}
5+
func main() {
6+
fmt.Println(max([]int{1, 5, 10, 25, 16, 1}))
7+
}
8+
9+
func max(arr []int) int {
1210

13-
subMax := max(list[1:])
14-
if list[0] > subMax {
15-
return list[0]
11+
if len(arr) == 1 {
12+
return arr[0]
1613
}
17-
return subMax
18-
}
1914

20-
func main() {
21-
fmt.Println(max([]int{1, 5, 10, 25, 16, 1}))
15+
if arr[0] > max(arr[1:]) {
16+
return arr[0]
17+
}
18+
return max(arr[1:])
2219
}

0 commit comments

Comments
 (0)