# Intersection of two arrays of large factory interview algorithm

Time：2021-9-23 Given two arrays, use a function to calculate the intersection of the two.

``````let num1 = [1,2,2,1];
let num2 = [2,2];
//Find [2,2]``````
``````let num1 = [4,9,5];
let num2 = [9,4,9,8,4];
//Find [4,9]``````

## Method 1

Save hash table

Since the same number may appear more than once in both arrays, you need to use a hash table to record the number of occurrences of each number. For the same number, the number of occurrences in the intersection is equal to the minimum number of occurrences of the number in the two arrays.

First, traverse the first array, record each number in the first array and the corresponding occurrence times in the hash table, and then traverse the second array. For each number in the second array, if there is this number in the hash table, add the number to the answer, and reduce the occurrence times of this number in the hash table.

In order to reduce the space complexity, first traverse the shorter array and record each number and the corresponding number of occurrences in the hash table, and then traverse the longer array to get the intersection.

``````const intersect = (num1, num2) => {
const map = {};
const res = [];
For (const N1 of num1) {// save the number of occurrences of num1
if (map[n1]) {
map[n1]++;
} else {
map[n1] = 1;
}
}
For (const N2 of num2) {// traverse num2 to see if any numbers appear in num1
const val = map[n2];
If (Val > 0) {// appears
res.push(n2);         //  Push res array
map[n2]--;            //  If you match one, you lose one
}
}
return res;
};``````

Operation results:

``````let num1 = [1,2,2,1];
let num2 = [2,2];
console.log(intersect(num1,num2));        //[2,2]``````

## Method 2

Double pointer mode

If two arrays are ordered, the intersection of two arrays can be calculated conveniently.

First sort the two arrays, and then traverse the two arrays with two pointers.

Initially, the two pointers point to the heads of the two arrays. Compare the numbers in the two arrays pointed to by the two pointers at a time. If the two numbers are not equal, move the pointer to the smaller number one bit to the right. If the two numbers are equal, add the number to the answer and move both pointers one bit to the right. When at least one pointer is out of the range of the array, the traversal ends.

``````const intersect = (num1, num2) => {
num1.sort((a, b) => a - b);
num2.sort((a, b) => a - b); //  Sort first so that duplicate elements appear adjacent
const res = [];
let p1 = 0;                  //  Two pointers
let p2 = 0;
While (P1 < num1. Length & & P2 < num2. Length) {// use & & because one of them is out of bounds, the intersection cannot be found
If (num1 [P1] > num2 [P2]) {// if P1 points to a large number, move P2 and expect to encounter the same number
p2++;
}Else if (num1 [P1] < num2 [P2]) {// similar
p1++;
}Else {// push the res array when the same pointer is encountered, and move the two pointers to the next one at the same time
res.push(num1[p1]);
p1++;
p2++;
}
}
return res;
};``````

Operation results:

``````let num1 = [4,9,5];
let num2 = [9,4,9,8,4];
console.log(intersect(num1,num2));        //[4,9]``````

## Method 3

The simplest double cycle

``````const intersect = (num1, num2) => {
const res = [];
for(let i=0;i<num1.length;i++){
for(let j=0;j<num2.length;j++){
if(num1[i] == num2[j]){
res.push(num1[i]);
num1.splice(i,1);
i--;        // The index needs to be moved forward after the element is deleted
num2.splice(j,1);
break;
}
}
}
return res;
};``````

No explanation ## Seven Python code review tools recommended

althoughPythonLanguage is one of the most flexible development languages at present, but developers often abuse its flexibility and even violate relevant standards. So PythoncodeThe following common quality problems often occur: Some unused modules have been imported Function is missing arguments in various calls The appropriate format indentation is missing Missing appropriate spaces before and after […]