Skip to content

Instantly share code, notes, and snippets.

@jakebromberg
Last active January 10, 2019 01:46
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 jakebromberg/5f9012796aca5f451702 to your computer and use it in GitHub Desktop.
Save jakebromberg/5f9012796aca5f451702 to your computer and use it in GitHub Desktop.
extension Sequence where Element == Int {
func pairsOfSum(k: Int) -> [(Int, Int)] {
var cache = [Int:Int]()
var result = [(Int, Int)]()
for i in self {
cache[i] = k - i
}
for (num, inv) in cache {
if cache[inv] != nil {
result.append((num, inv))
}
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment