[LeetCode] 2215. Find the Difference of Two Arrays

Semidragon·2023년 11월 9일
0

CodingTest

목록 보기
18/80

1. Question

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 nums2. 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] = [].

2. Thoughts

Simple.
Find the common sets of 2 list,
return each list - common set.
Probably easy due python built-in functions.

3. Tips learned

To find all the common elements between two lists in Python, you can use the set intersection operation, which is efficient and straightforward. Here's an example:

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

# Convert lists to sets and find the intersection
common_elements = set(list1).intersection(list2)

# Convert the set back to a list, if you need a list structure
common_elements_list = list(common_elements)

print(common_elements_list)

The set.intersection method or & operator finds the common elements between two sets. If you convert your lists to sets and then use intersection, you'll get the set of elements that are in both lists.

If you need to maintain the order and duplicate elements, you would have to use a different approach, such as list comprehensions:

common_elements = [element for element in list1 if element in list2]

This list comprehension iterates through list1 and includes only those elements that are also in list2. Note that this method is less efficient than using sets, especially for large lists, because it has to search through list2 for every element in list1.

4. My solution

class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        set_nondistinct = set(nums1).intersection(nums2)
        return [list(set(nums1) - set_nondistinct), list(set(nums2) - set_nondistinct)]
202/202 cases passed (152 ms)
Your runtime beats 88.39 % of python3 submissions
Your memory usage beats 86.42 % of python3 submissions (16.6 MB)

5. AI Solution and Improvements

Your solution is efficient and makes good use of Python's set operations to find the distinct elements in both arrays. However, the code can be slightly simplified by directly subtracting the sets without the need to find the intersection first.

Here's a simplified version of your code:

class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        return [list(set(nums1) - set(nums2)), list(set(nums2) - set(nums1))]

In this version, set(nums1) - set(nums2) creates a set of elements that are in nums1 but not in nums2, and vice versa for set(nums2) - set(nums1). The list() function then converts these sets back into lists. This solution is concise and takes full advantage of Python's set operations to produce the desired result.

Turns out didn't even need to find the common sets

profile
Semidragon's network [CS undergrad @ Sungkyunkwan University | Networks + System @ CSI]

0개의 댓글