Advertisement

Intersection of Two Arrays II - LeetCode 350 Solution

Intersection of Two Arrays II - Complete Solution Guide

Intersection of Two Arrays II is LeetCode problem 350, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given two integer arrays nums1 and nums2 , return an array of their intersection . Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order . 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] Explanation: [9,4] is also accepted. Constraints: 1 <= nums1.length, nums2.length <= 1000 0 <= nums1[i], nums2[i] <= 1000 Follow up: What if the given array i

Detailed Explanation

The problem asks us to find the intersection of two integer arrays, `nums1` and `nums2`. The intersection should include each number that appears in *both* arrays, and the number of times it appears in the result must match the *minimum* number of times it appears in either `nums1` or `nums2`. The order of the elements in the result array doesn't matter. The constraints specify that the array lengths are at most 1000, and the values in the arrays are between 0 and 1000.

Solution Approach

The solution uses a hash table (dictionary in Python) to store the frequency of each element in the first array (`nums1`). Then, it iterates through the second array (`nums2`). For each element in `nums2`, it checks if the element exists in the hash table and if its count is greater than 0. If both conditions are true, it appends the element to the result array and decrements its count in the hash table. This ensures that the number appears in the result as many times as it appears in both arrays. Finally, the result array is returned.

Step-by-Step Algorithm

  1. Step 1: Create an empty hash table (dictionary) called `count`.
  2. Step 2: Iterate through `nums1`. For each element `num` in `nums1`, increment its count in the `count` hash table. If `num` is not already in `count`, initialize its count to 1.
  3. Step 3: Create an empty list called `result` to store the intersection elements.
  4. Step 4: Iterate through `nums2`. For each element `num` in `nums2`:
  5. Step 5: Check if `num` exists as a key in the `count` hash table AND if `count[num]` is greater than 0.
  6. Step 6: If both conditions in Step 5 are true:
  7. Step 7: Append `num` to the `result` list.
  8. Step 8: Decrement `count[num]` by 1.
  9. Step 9: After iterating through all elements in `nums2`, return the `result` list.

Key Insights

  • Insight 1: We need to count the frequency of each number in at least one of the arrays to determine how many times it appears in the intersection.
  • Insight 2: A hash table (or dictionary) is an efficient data structure for storing the frequency counts of the numbers.
  • Insight 3: We need to iterate through the second array and check if each number is present in the hash table and if its count is greater than zero. If so, we add it to the result and decrement its count in the hash table.

Complexity Analysis

Time Complexity: O(m+n)

Space Complexity: O(min(m,n))

Topics

This problem involves: Array, Hash Table, Two Pointers, Binary Search, Sorting.

Companies

Asked at: Yandex.