Created
January 21, 2020 18:47
-
-
Save isisnaomi/048c9b8df057f0b4723e571ebb8e80fa to your computer and use it in GitHub Desktop.
largestFour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## 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