Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:
answer[0] is a list of all distinct integers in nums1 which are not present in nums2.answer[1] is a list of all distinct integers in nums2 which are not present in nums1.Note that the integers in the lists may be returned in any order.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4,6] Output: [[1,3],[4,6]] Explanation: For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3]. For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums1. Therefore, answer[1] = [4,6].
Example 2:
Input: nums1 = [1,2,3,3], nums2 = [1,1,2,2] Output: [[3],[]] Explanation: For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] == nums1[3], their value is only included once and answer[0] = [3]. Every integer in nums2 is present in nums1. Therefore, answer[1] = [].
Constraints:
1 <= nums1.length, nums2.length <= 1000-1000 <= 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:
The goal is to find which numbers are unique to each of two lists. The brute force method involves taking each number from the first list and checking it against every single number in the second list, and then doing the same thing in reverse for the second list.
Here's how the algorithm would work step-by-step:
def find_difference_brute_force(first_list, second_list):
unique_to_first_list = []
unique_to_second_list = []
# Iterate through the first list to find numbers not present in the second list.
for first_list_number in first_list:
is_number_found_in_second_list = False
for second_list_number in second_list:
if first_list_number == second_list_number:
is_number_found_in_second_list = True
break
# If the number was not found after checking all elements, it's unique to the first list.
if not is_number_found_in_second_list:
if first_list_number not in unique_to_first_list:
unique_to_first_list.append(first_list_number)
# Now, iterate through the second list to find numbers not present in the first list.
for second_list_number in second_list:
is_number_found_in_first_list = False
for first_list_number in first_list:
if second_list_number == first_list_number:
is_number_found_in_first_list = True
break
# Similarly, if the number was not found, it is unique to the second list.
if not is_number_found_in_first_list:
if second_list_number not in unique_to_second_list:
unique_to_second_list.append(second_list_number)
return [unique_to_first_list, unique_to_second_list]The most efficient way to solve this is to quickly check which numbers are present in each group. By first making a unique collection of numbers for each of the two starting groups, we can avoid repetitive checks and instantly see if a number from one group exists in the other.
Here's how the algorithm would work step-by-step:
class Solution:
def findDifference(self, first_list_of_numbers: list[int], second_list_of_numbers: list[int]) -> list[list[int]]:
# Use sets for efficient O(1) average time complexity lookups.
unique_numbers_from_first_list = set(first_list_of_numbers)
unique_numbers_from_second_list = set(second_list_of_numbers)
distinct_in_first_list = []
distinct_in_second_list = []
# Iterate through the first set to find numbers not present in the second.
for number in unique_numbers_from_first_list:
if number not in unique_numbers_from_second_list:
distinct_in_first_list.append(number)
# Iterate through the second set to find numbers not present in the first.
for number in unique_numbers_from_second_list:
if number not in unique_numbers_from_first_list:
distinct_in_second_list.append(number)
return [distinct_in_first_list, distinct_in_second_list]| Case | How to Handle |
|---|---|
| One or both input arrays are empty. | The solution should correctly return the distinct elements of the non-empty array in one list and an empty list for the other. |
| Both input arrays are identical, containing the same set of elements. | The algorithm should return two empty lists as there is no difference between the arrays. |
| Input arrays contain duplicate values. | Using sets naturally handles duplicates, ensuring the output lists contain only distinct integers. |
| Input arrays have no common elements. | The result should be two lists containing all the distinct elements of the original arrays respectively. |
| Input arrays contain negative numbers and zeros. | A hash set based solution correctly handles negative numbers and zeros just like any other integer. |
| One array is a complete subset of the other. | One of the resulting lists will be empty, while the other contains elements from the larger set not present in the smaller subset. |
| Arrays are very large, approaching maximum constraints (e.g., 1000 elements). | A solution using hash sets is efficient with an average time complexity of O(N+M), which scales well for large inputs. |
| Input values are at the extreme ends of the allowed integer range. | Standard hash set implementations in most languages can manage the full range of integer values without issue. |