Skip to content

Instantly share code, notes, and snippets.

@jpmcglone
Created May 28, 2019 19:43
Show Gist options
  • Save jpmcglone/f1a45ddf31a91794667db0da9e2e99a8 to your computer and use it in GitHub Desktop.
Save jpmcglone/f1a45ddf31a91794667db0da9e2e99a8 to your computer and use it in GitHub Desktop.
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
}
@jpmcglone
Copy link
Author

let dictionary = ["A" : 3, "Z": 6, "D": 7]
let key = highestKey(of: dictionary)
print(key ?? "n/a")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment