88.6% beatable ✨ || Easy JS Sol. || Understandable (Sets) Approach 💯🧑‍💻
Intuition\n Describe your first thoughts on how to solve this problem. \n\n- The below code implements a function called findDifference that takes in two arrays
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n- The below code implements a function called **findDifference** that takes in two arrays, nums1 and nums2, and returns an array containing **two arrays**. The **two arrays** inside the returned array are the disjoint elements of **nums1** and **nums2**.\n\n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. To accomplish the result, the code creates ***two sets***, **set1** and **set2**, to store the ***unique elements*** of **nums1** and **nums2**, respectively.\n2. It then creates two ***empty arrays***, **result1** and **result2**, to store the disjoint elements of **nums1** and **nums2**, respectively.\n3. Next, the code iterates over the union of the two sets using the ***spread operator ([...set1, ...set2])***. \n4. For each element, it checks if the element is in **set2** and if so, pushes it into **result1**. \n5. Similarly, if the element is in **set1**, it is pushed into **result2**.\n6. Finally, the code **returns** an array containing **result1** and **result2**.\n\n##### - Overall, the approach of the above code is to use ***sets*** to efficiently find the unique elements in the two input arrays and then compare them to find the disjoint elements.\n\n\n\n\n\n\n\n\n\n\n\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n1. Here n is the length of the longer of the two input arrays (nums1 and nums2). \n2. This is because the code iterates over the union of the two sets, which takes at most n iterations since each element is only visited once. \n3. Set operations (such as has) have an average time complexity of **O(1)**, so they don\'t add any additional complexity to the algorithm.\n\n\n\n\n\n\n\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n1. Since the two sets and two arrays created have a maximum size of **n** ***(the size of the longer of the two input arrays)***.\n\n\n\n\n\n\n\n\n\n\n# Code\n```\n/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number[][]}\n */\nvar findDifference = function(nums1, nums2) {\n    \n    // Create two empty Sets to store unique elements of nums1 and nums2\n    \n    let set1 = new Set(nums1);\n    \n    let set2 = new Set(nums2);\n    \n    // Create two arrays to store the disjoint elements\n    \n    let result1 = [];\n    \n    let result2 = [];\n    \n    // Iterate over nums1 and nums2 together and add the disjoint elements to the result arrays\n    \n    for (let num of [...set1, ...set2]) {\n        \n        if (!set2.has(num)) {\n            \n            result1.push(num);\n        }\n        \n        if (!set1.has(num)) {\n            \n            result2.push(num);\n        }\n    }\n    \n    // Return the disjoint elements in an array\n    \n    return [result1, result2];  \n};\n```\n![cat img for upvote on LC.jpeg](https://assets.leetcode.com/users/images/f1df6dd1-c032-4294-b13e-a59cfc7f9541_1683234123.083215.jpeg)\n\n
['Array', 'Hash Table', 'JavaScript']