Advertisement
Niloy007

Intersection of Two Arrays

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