---
# System prepended metadata

title: '[Two Sum](https://leetcode.com/problems/two-sum/)'
tags: [Leetcode, Easy, Arrays and Hashing]

---

# [Two Sum](https://leetcode.com/problems/two-sum/)
###### tags: `Leetcode`, `Easy`, `Arrays and Hashing`

## Approach
* Initialize a HashMap
* Iterate through every number in list
    * diff = target - nums[index]
    * if diff is in HashMap - return list of indices
    * otherwise add to HashMap: key = nums[index], value = index
* return [] if no match is found for diff

## Asymptotic Analysis
### Time Complexity: **O(n)**
### Space Complexity: **O(n)**

## Code
``` python
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hMap = {}
        for index in range(len(nums)):
            diff = target - nums[index]
            if diff in hMap:
                return [index, hMap[diff]]
            hMap[nums[index]] = index
        return []
```