Advertisement
momo2345

intersection of 2 arrays

Aug 27th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
  4.         sort(nums2.begin(),nums2.end());
  5.         vector<int>r;
  6.         for(auto u : nums1)
  7.         {
  8.             if(binary_search(nums2.begin(),nums2.end(),u))
  9.               r.push_back(u);
  10.         }
  11.         sort(r.begin(),r.end());
  12.         int s=unique(r.begin(),r.end())-r.begin();
  13.         while(r.size()>s) r.pop_back();
  14.         return r;
  15.     }
  16. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement