2215. Find the Difference of Two Arrays
Содержание
Problem Statement
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.
Naive Solution
A naive approach would be to iterate over each element of the first array and check whether it’s in the second array. Similarly, iterate over each element of the second array and check whether it’s in the first array. This approach will take O(n*m) time, where n is the size of the first array and m is the size of the second array. This isn’t efficient for large input sizes.
Hints & Tips
We can solve this problem more efficiently by using Python’s built-in set data structure. A set is a collection of unique elements and allows for O(1) time complexity for lookup operations.
Approach
- Convert both arrays to sets.
- Find the difference between the two sets in both directions.
- Convert the results to lists and return them.
Steps
- Convert
nums1
to a setset1
andnums2
to a setset2
. - Find the difference between
set1
andset2
and convert it to a list. This will give us all the elements that are inset1
but not inset2
. - Find the difference between
set2
andset1
and convert it to a list. This will give us all the elements that are inset2
but not inset1
. - Return the results as a list of two lists.
Solution
def findDifference(nums1, nums2):
set1 = set(nums1)
set2 = set(nums2)
diff1 = list(set1 - set2)
diff2 = list(set2 - set1)
return [diff1, diff2]