nums
where nums[i]
is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums
sorted in ascending order.
Example 1:
Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] Output: [3,4] Explanation: The only integers present in each of nums[0] = [3,1,2,4,5], nums[1] = [1,2,3,4], and nums[2] = [3,4,5,6] are 3 and 4, so we return [3,4].
Example 2:
Input: nums = [[1,2,3],[4,5,6]] Output: [] Explanation: There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
Constraints:
1 <= nums.length <= 1000
1 <= sum(nums[i].length) <= 1000
1 <= nums[i][j] <= 1000
nums[i]
are unique.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 method for finding common elements in multiple lists involves checking every possible number to see if it appears in all of them. We essentially go through each number in one list and compare it to every number in all other lists. If a number is present in all lists, we've found a common element.
Here's how the algorithm would work step-by-step:
def intersection_of_multiple_arrays(arrays):
if not arrays:
return []
first_array = arrays[0]
common_elements = []
for number_in_first_array in first_array:
is_common = True
# Iterate through the rest of the arrays
for other_array in arrays[1:]:
#Check if number exists in the other array
if number_in_first_array not in other_array:
is_common = False
break
# If the number is in all the arrays, add it to the result
if is_common:
common_elements.append(number_in_first_array)
return common_elements
The best way to find common elements across multiple lists is to count how often each element appears. Then, identify elements that show up in every single list. This avoids comparing every single element in every list with all the others.
Here's how the algorithm would work step-by-step:
def find_intersection_of_arrays(list_of_lists):
element_counts = {}
# Count occurrences to avoid direct comparisons.
for current_list in list_of_lists:
for element in current_list:
if element in element_counts:
element_counts[element] += 1
else:
element_counts[element] = 1
intersection_result = []
# Identify elements present in ALL lists.
for element, count in element_counts.items():
if count == len(list_of_lists):
intersection_result.append(element)
return intersection_result
Case | How to Handle |
---|---|
Null or empty input list of arrays | Return an empty list indicating no intersection exists. |
One or more input arrays are null or empty | Treat empty arrays as contributing nothing to the intersection, so the result is empty. |
Arrays with extremely large sizes, approaching memory limits | Use a memory-efficient approach like counting occurrences rather than repeatedly creating new sets. |
Arrays containing duplicate elements | The intersection should only contain unique elements, even if duplicates exist in the input arrays. |
Arrays with very different lengths (e.g., one array has 1 element, others have millions) | Iterate through the smallest array to minimize the number of checks required. |
Arrays contain very large numbers potentially leading to overflow if summing for comparison | Avoid summing; compare elements directly or use a hash table to avoid arithmetic operations. |
No common elements exist between any of the arrays | The solution should return an empty list in this case, indicating no intersection. |
Arrays contain negative numbers, zeros, and positive numbers | The solution must correctly handle all number ranges, without type-specific errors. |