# 349. Intersection of Two Arrays

## 1.問題

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LKWlLG5z4NrHD4-0Q55%2F-LKWlsxrXaAvMrQJQQJS%2F2018082201.jpg?alt=media\&token=63c2a092-3387-45f8-b3d8-a4b0da2bb1a1)

## 2.想法

## 3.程式碼

```
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ans;
        map<int, int> cnt;
        for (int i = 0; i < nums1.size(); i++) {
            if (cnt.find(nums1[i]) == cnt.end()) {
                cnt.insert({nums1[i], 1});
            }  
        }
        for (int i = 0; i < nums2.size(); i++) {
            if (cnt.find(nums2[i]) != cnt.end()) {
                ans.push_back(nums2[i]);
                cnt.erase(nums2[i]);
            } 
        }
        return ans;
    }
};
```

## 4.Performance

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LKWlLG5z4NrHD4-0Q55%2F-LKWmAwP8CeYEvcXwaWm%2F2018082202.jpg?alt=media\&token=024cb3d4-d201-4182-b0aa-bfb02d267da8)
