Skip to content

Instantly share code, notes, and snippets.

@IhwanID
Created January 31, 2021 06:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save IhwanID/ca6d120f3cfa5d48dc83ee791a5ed22a to your computer and use it in GitHub Desktop.
Two Sum in Swift
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var dictionary = [Int:Int]();
for index in 0 ..< nums.count {
let complement = target - nums[index];
if dictionary.keys.contains(complement) && dictionary[complement] != index {
return [dictionary[complement]!,index];
}
dictionary[nums[index]] = index
}
return [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment