diff --git a/dart/two_sum.dart b/dart/two_sum.dart new file mode 100644 index 0000000..d5c6346 --- /dev/null +++ b/dart/two_sum.dart @@ -0,0 +1,22 @@ +/* +Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + +You may assume that each input would have exactly one solution, and you may not use the same element twice. +*/ + +class Solution { + List twoSum(List nums, int target) { + final Map seen = {}; + + for (int i = 0; i < nums.length; i++) { + final int complement = target - nums[i]; + if (seen.containsKey(complement)) { + return [seen[complement]!, i]; + } + + seen[nums[i]] = i; + } + + return []; + } +}