> For the complete documentation index, see [llms.txt](https://zcjian.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zcjian.gitbook.io/project/array/two-sum.md).

# Two Sum

LeetCode 1 (allow duplicate numbers)

```python
def twoSum(nums, target):
    # nums = [value1, value2, value3 ...]
    nums.sort() 
    # dict = {value1 : position1, value2 : position2}
        dict = {}
        res = []
        i = 0
        while i < len(nums):
            # 若 i > 0 且 值重覆了, 則繼續往下找
            if i > 0 and nums[i] == nums[i-1]:
                i = i + 1
                continue 
            x = nums[i]
            if x in dict:
                res.append((dict[x], i))
            dict[target - x] = i
            i = i + 1
        return res

twoSum([-3, -2, 1, 2, 2, 3, 4], 1)
```
