4-line C++ solution with unordered_set


  • 0
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
          unordered_set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()); vector<int> res;
          if (s1.size() < s2.size()) { for (int x : s1) if (s2.count(x)) res.push_back(x); }
          else { for (int x : s2) if (s1.count(x)) res.push_back(x); }
          return res;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.