Find the Difference of Two Arrays
Here is a simple Python solution using sets.
Sets in Python are used to store multiple items in a single variable and they are one of the data types that Python offers. Set items are unordered, unchangeable, and do not allow duplicate values.
In this solution, we convert the input arrays to sets and then use the set difference operation to find the elements that are in one set but not the other. Here is the code:


In this code:
We convert
nums1
andnums2
to setsset1
andset2
. This removes any duplicate elements and allows us to perform set operations.We use the

operator to compute the difference between the sets.set1  set2
gives us all elements that are inset1
but not inset2
, andset2  set1
gives us all elements that are inset2
but not inset1
.We convert the result back to a list, as the problem requires us to return a list of lists.
The time complexity of this solution is O(n), where n is the total number of elements in nums1
and nums2
. This is because converting a list to a set takes linear time, and the set difference operation also takes linear time. The space complexity is also O(n), for storing the sets.