Skip to content

Instantly share code, notes, and snippets.

@khawajafarooq
Created March 10, 2018 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khawajafarooq/bb493f1c43e912b1df797e6ee9ec578b to your computer and use it in GitHub Desktop.
Save khawajafarooq/bb493f1c43e912b1df797e6ee9ec578b to your computer and use it in GitHub Desktop.
Find two maximum numbers from an array
import Foundation
func findTwoMax(_ array: [Int]) -> (Int, Int)? {
guard array.count > 1 else {
return nil
}
var max = Int.min
var secondMax = Int.min
for i in 0..<array.count {
if max < array[i] {
secondMax = max
max = array[i]
}
else if secondMax < array[i] {
secondMax = array[i]
}
}
return (max, secondMax)
}
// Test
findTwoMax([1,12,3,7,6,5,8,14])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment