Created
January 31, 2021 06:26
-
-
Save IhwanID/ca6d120f3cfa5d48dc83ee791a5ed22a to your computer and use it in GitHub Desktop.
Two Sum in Swift
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 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