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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding common elements in two lists is like comparing each item from one list against every item in the other. If we find a match, we add it to our results. We continue this process until we've checked all possible pairs of items.
Here's how the algorithm would work step-by-step:
def intersection_of_two_arrays_brute_force(first_array, second_array):
intersection_result = []
for first_array_element in first_array:
# For each element, check against every element in the second array
for second_array_element in second_array:
if first_array_element == second_array_element:
# Ensure we don't add duplicates to the intersection
if first_array_element not in intersection_result:
intersection_result.append(first_array_element)
return intersection_result
The most efficient way to find common numbers between two lists is to first organize each list individually. Then, we can easily compare them to quickly pinpoint the shared numbers without unnecessary checks.
Here's how the algorithm would work step-by-step:
def intersection_of_two_arrays(first_array, second_array):
first_array.sort()
second_array.sort()
index_first_array = 0
index_second_array = 0
intersection_result = []
while index_first_array < len(first_array) and index_second_array < len(second_array):
if first_array[index_first_array] == second_array[index_second_array]:
# Found a common element, add to result.
intersection_result.append(first_array[index_first_array])
index_first_array += 1
index_second_array += 1
elif first_array[index_first_array] < second_array[index_second_array]:
# Advance pointer of the smaller element.
index_first_array += 1
else:
# Advance pointer of the smaller element.
index_second_array += 1
return intersection_result
Case | How to Handle |
---|---|
One or both input arrays are null or empty | Return an empty array if either input is null or empty to avoid NullPointerExceptions and undefined behavior. |
Arrays contain very large numbers (potential integer overflow) | Use long data types or appropriate data structures if the numbers are likely to exceed the integer limits. |
Arrays contain duplicate numbers; ensure only unique intersections are returned | Use a Set to store the intersection to guarantee uniqueness, preventing duplicate entries. |
One array is extremely large, while the other is very small | Iterate through the smaller array and check for existence in the larger array (using a HashSet for O(1) lookups in the larger array) to optimize performance. |
Arrays contain negative numbers, zeros, and positive numbers | The HashSet or other comparison strategies should correctly handle negative numbers and zeros without additional logic. |
No common elements exist between the two arrays | The function should return an empty array, demonstrating that no intersections were found. |
Arrays are identical and contain only one element | The function should return an array containing that single element. |
Arrays contain extreme boundary values (Integer.MAX_VALUE, Integer.MIN_VALUE) | The solution should handle these values correctly without causing overflows or unexpected behavior in the comparison or hashing process. |