Intersection of Two Arrays - Complete Solution Guide
Intersection of Two Arrays is LeetCode problem 349, 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 be unique and you may return the result in any order . 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] Explanation: [4,9] is also accepted. Constraints: 1 <= nums1.length, nums2.length <= 1000 0 <= nums1[i], nums2[i] <= 1000
Detailed Explanation
The problem asks you to find the intersection of two integer arrays, `nums1` and `nums2`. The intersection is the set of elements that are present in both arrays. The output should contain only unique elements, and the order of elements doesn't matter. The input arrays can contain duplicate elements, but the output must not. There are constraints on the size of the input arrays (maximum 1000 elements) and the range of values within the arrays (0 to 1000 inclusive).
Solution Approach
The provided solutions use sets (or hash sets/unordered sets) to efficiently find the intersection. First, one of the input arrays is converted into a set. This set is then iterated through and checked for the presence of every number in the second array. If a number from the second array is found in the first set it’s added to the result set. Finally, the result set is converted back into an array for output.
Step-by-Step Algorithm
- Step 1: Create a set from one of the input arrays (e.g., `nums1`). This eliminates duplicate elements.
- Step 2: Iterate through the second input array (e.g., `nums2`).
- Step 3: For each element in `nums2`, check if it exists in the set created from `nums1`.
- Step 4: If the element exists in the set, add it to a new set (or other structure) to store the intersection.
- Step 5: Convert the resulting set of unique intersecting elements into an array and return it.
Key Insights
- Insight 1: Using sets (or hash tables) is crucial for efficiently finding the intersection because sets inherently store only unique elements and provide fast lookups (O(1) on average).
- Insight 2: The optimal approach involves iterating through one array and checking for the presence of each element in a set created from the other array. This avoids nested loops, which would lead to a less efficient solution.
- Insight 3: The provided Python solution leverages the built-in set operations for a concise and efficient solution, but understanding the underlying set operations is important for adapting this to other languages.
Complexity Analysis
Time Complexity: O(m+n)
Space Complexity: O(m+n)
Topics
This problem involves: Array, Hash Table, Two Pointers, Binary Search, Sorting.
Companies
Asked at: CVENT, Criteo, IBM, J.P. Morgan, LinkedIn, MongoDB, Nvidia, PayPal, Two Sigma, Wix, Yandex, tcs.