Created
May 28, 2019 19:43
-
-
Save jpmcglone/f1a45ddf31a91794667db0da9e2e99a8 to your computer and use it in GitHub Desktop.
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
func highestKey<T, N: Comparable>(of dictionary: Dictionary<T, N>) -> T? { | |
var highestKey: T? | |
var highestValue: N? | |
for key in dictionary.keys { | |
let value = dictionary[key] | |
if highestValue == nil || value! > highestValue! { | |
highestKey = key | |
highestValue = value | |
} | |
} | |
return highestKey | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let dictionary = ["A" : 3, "Z": 6, "D": 7]
let key = highestKey(of: dictionary)
print(key ?? "n/a")