Skip to content

Instantly share code, notes, and snippets.

@isisnaomi
Created January 21, 2020 18:47
Show Gist options
  • Save isisnaomi/048c9b8df057f0b4723e571ebb8e80fa to your computer and use it in GitHub Desktop.
Save isisnaomi/048c9b8df057f0b4723e571ebb8e80fa to your computer and use it in GitHub Desktop.
largestFour
//## Largest Four
//
//**Challenge**
//
//Have the function LargestFour(**arr**) take the array of integers stored in **arr**, and find the four largest elements and return their sum.
//
//**Sample Test Cases**
//
//Input:1, 1, 1, -5
//
//Output:-2
//
//Input:0, 0, 2, 3, 7, 1
//
//Output:13
func largestFour(arr: [Int]) -> Int {
var sum = 0
var sortedArr = arr.sorted()
for i in 1...4 {
sum = sum + arr[arr.count - i]
}
return sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment