Here are two integer arrays  nums1 and nums2
, Please return the intersection of two arrays in the form of array . Returns the number of occurrences of each element in the result , It should be consistent with the number of times the element appears in both arrays （ If the occurrence times are inconsistent , Then consider taking the smaller value ）. The order of output results can be ignored .

Example 1：

input ：nums1 = [1,2,2,1], nums2 = [2,2]
output ：[2,2]
Example 2:

input ：nums1 = [4,9,5], nums2 = [9,4,9,8,4]
output ：[4,9]

Tips ：

1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000

method 1：hash Tabular method
class Solution { public: vector<int> intersect(vector<int>& nums1,
vector<int>& nums2) { map<int,int> hashmap; vector<int> arr;
// Use one map Storing each element in the first data for(int i = 0 ;i < nums1.size(); i++){
hashmap[nums1[i]]++; } // And then Map to nums2 Compare output for(int i = 0 ;i < nums2.size();
i++){ // If an element is found map<int, int>::iterator temp; temp = hashmap.find(nums2[i]);
// Here is the comparison of times if(temp != hashmap.end() && temp->second != 0){
arr.push_back(temp->first); temp->second--; } } return arr; } };
method 2： Double finger needling

Here first 2 Arrays are sorted first , Then conduct mobile troubleshooting
bool cmp(int a,int b){return a<b;} class Solution { public: vector<int>
intersect(vector<int>& nums1, vector<int>& nums2) { vector<int> arr;
// First, yes 2 Arrays are sorted in descending order sort(nums1.begin(),nums1.end(),cmp);
sort(nums2.begin(),nums2.end(),cmp); int i = 0,j = 0; while(i != nums1.size()
&& j != nums2.size()){ if(nums1[i] < nums2[j]){ i++; }else if(nums1[i] >
nums2[j]){ j++; }else{ arr.push_back(nums1[i]); i++; j++; } } return arr; } };

Technology
Daily Recommendation