Update 04_recursive_max.swift
It wasn't working when the array was empty or when it had only one element.
This commit is contained in:
committed by
Aditya Bhargava
parent
86785a0659
commit
885c5f8b19
@@ -3,6 +3,8 @@ import Foundation
|
|||||||
func max(list : [Int]) -> Int {
|
func max(list : [Int]) -> Int {
|
||||||
if list.count == 2 {
|
if list.count == 2 {
|
||||||
return (list[0] > list[1]) ? list[0] : list[1]
|
return (list[0] > list[1]) ? list[0] : list[1]
|
||||||
|
} else if list.count < 2 {
|
||||||
|
return list.first ?? 0
|
||||||
}
|
}
|
||||||
var tempArray = list
|
var tempArray = list
|
||||||
tempArray.remove(at: 0)
|
tempArray.remove(at: 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user