class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> m(nums1.begin(), nums1.end());
vector<int> res;
for (auto a : nums2)
if (m.count(a)) {
res.push_back(a);
m.erase(a);
}
return res;
}
};
8ms concise C++ using unordered_set

@s961206 it's stl vector's function to get iterator, its usage like pointer in array but not this.

A possible improvement. Combine "erase" and "count" together to save some time.
#include <unordered_set> class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { if (nums1.empty()  nums2.empty()){ return std::vector<int>(); } std::unordered_set<int> set{nums1.cbegin(), nums1.cend()}; std::vector<int> intersections; for (auto n: nums2){ if (set.erase(n) > 0){ // if n exists in set, then 1 is returned and n is erased; otherwise, 0. intersections.push_back(n); } } return intersections; } };