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 <= 10000 <= nums1[i], nums2[i] <= 1000When 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:
Imagine you have two separate buckets filled with different items, and you want to find out which items appear in both buckets. The brute force approach involves picking up every single item from the first bucket and comparing it against every single item in the second bucket one by one.
Here's how the algorithm would work step-by-step:
def intersection_of_two_arrays(first_bucket, second_bucket):
shared_items_collection = []
# We iterate through every item to ensure all possible matches are evaluated
for first_bucket_item in first_bucket:
for second_bucket_item in second_bucket:
if first_bucket_item == second_bucket_item:
# We check presence to avoid duplicates in our final shared collection
if first_bucket_item not in shared_items_collection:
shared_items_collection.append(first_bucket_item)
# Returning the final collection gives us the complete intersection result
return shared_items_collectionTo efficiently find the common items between two collections, the optimal approach relies on a mental filter system. Instead of constantly comparing every item against every other item, we can first make a unique membership list of one collection so we can instantly check items from the second collection.
Here's how the algorithm would work step-by-step:
def intersection(first_array, second_array):
# Using a set provides O(1) average time complexity for membership lookup operations.
unique_tracker_set = set(first_array)
collected_intersection = []
for current_item in second_array:
# Checking existence and removing ensures each common element is counted only once.
if current_item in unique_tracker_set:
collected_intersection.append(current_item)
unique_tracker_set.remove(current_item)
return collected_intersection| Case | How to Handle |
|---|---|
| One or both input arrays are empty or null | Return an empty array immediately since no intersection can exist between empty datasets |
| Arrays containing duplicate values | Use a set data structure to automatically filter out duplicates and ensure the final intersection contains only unique elements |
| Arrays with completely disjoint values having no intersection | The result collection remains empty and the algorithm returns it safely without throwing errors |
| Arrays containing negative numbers and zero | Standard hash set lookups handle negative integers and zero identically to positive numbers without any special offset logic |
| Extremely large arrays exceeding typical memory limits | Stream or two-pointer sorting approaches can be utilized to process elements iteratively rather than loading everything into memory |
| One array is significantly larger than the other | Convert the smaller array into a hash set first to minimize memory consumption and optimize lookup time |
| Inputs with extreme boundary integer values | Standard hash set implementations handle 32-bit or 64-bit integer limits natively without arithmetic overflow risks |
| Both arrays are identical and contain all duplicate elements | Using sets for both arrays reduces them to unique sets before finding the intersection, returning the deduplicated single instance of each element |