“This is the 14th day of my participation in the First Challenge 2022.


This paper presents a two-pointer solution to the intersection of two arrays (I, II)

Intersection I of two arrays

Given two arrays nums1 and nums2, return their intersection. Each element in the output must be unique. We can ignore the order of the output.

Example 1: input: nums1 = [1,2,2,1], nums2 = [2,2] output: [2] example 2: input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] output: [9,4] description: [4,9] is also passableCopy the code

Note:

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

Answer:

If two arrays are ordered, the intersection of the two arrays can be obtained using the two-pointer method.

The two arrays are sorted first, then iterated through with two Pointers. It is predictable that the number of elements added to the answer array must be increasing. To ensure the uniqueness of the added elements, we need an additional record variable, pre, to represent the last element added to the answer array.

Initially, two Pointers point to the heads of two arrays. Each time you compare the numbers in the two arrays to which the two Pointers point, if the two numbers are not equal, move the pointer to the smaller number one to the right. If the two numbers are equal and not equal to pre, add that number to the answer and update the pre variable, moving both Pointers one to the right. The traversal ends when at least one pointer is out of range.

JavaScript implementation:

var intersection = function(nums1, nums2) { nums1.sort((x, y) => x - y); nums2.sort((x, y) => x - y); const length1 = nums1.length, length2 = nums2.length; let index1 = 0, index2 = 0; const intersection = []; while (index1 < length1 && index2 < length2) { const num1 = nums1[index1], num2 = nums2[index2]; If (num1 === num2) {// Ensure that the element is unique if (! intersection.length || num1 ! == intersection[intersection.length - 1]) { intersection.push(num1); } index1++; index2++; } else if (num1 < num2) { index1++; } else { index2++; } } return intersection; };Copy the code

Intersection of two arrays II

A. I b. I C. I D. I

Given two integer arrays nums1 and nums2, please return the intersection of the two arrays as an array. The number of occurrences of each element in the returned result should be the same as the number of occurrences of the element in both arrays (if not, consider a smaller value). You can ignore the order of the output.

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]Copy the code

Answer:

If two arrays are ordered, the intersection of the two arrays can be obtained using the two-pointer method.

The two arrays are sorted first, then iterated through with two Pointers.

Initially, two Pointers point to the heads of two arrays. Each time you compare the numbers in the two arrays to which the two Pointers point, if the two numbers are not equal, move the pointer to the smaller number one to the right, if the two numbers are equal, add that number to the answer, and move both Pointers one to the right. The traversal ends when at least one pointer is out of range.

JavaScript implementation:

/** * @param {number[]} nums1 * @param {number[]} nums2 * @return {number[]} */ var intersect = function(nums1, Nums1.sort ((a,b) => a-b) nums2.sort((a,b) => a-b) let res = [] let key1 = 0, key2 = 0, While (key1 < nums1.length && key2 < nums2.length){// Judge the size of nums1[key1] and nums2[key2], If (nums1[key1] < nums2[key2]) key1++ else if(nums1[key1] > nums2[key2]) key2++ else{res[index++] = nums1[key1] key1++ key2++ } } return res };Copy the code

I’m Nuggets Anthony, output exposure input, technical insight into life, goodbye ~