Own Solution
I don't have much experience to use set and dictionary. So
class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
nums1 = set(nums1)
nums2 = set(nums2)
dict1,dict2 = {} ,{}
result1,result2 = [],[]
for i in nums1 :
dict1.update({f'{i}':1})
for i in nums2 :
dict2.update({f'{i}':1})
for number in nums1:
if dict2.get(f'{number}') == None:
result1.append(number)
for number in nums2:
if dict1.get(f'{number}') == None:
result2.append(number)
return result1, result2
Chatgpt Solution:
directly use set operations to find the symmetric difference between the two sets created from the input lists.
class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
set1 = set(nums1)
set2 = set(nums2)
result1 = list(set1 - set2) # Elements in nums1 but not in nums2
result2 = list(set2 - set1) # Elements in nums2 but not in nums1
return result1, result2