🔍 [VIDEO] Find the 🔢 Difference of Two Arrays
Intuition\n\nOur first thoughts on solving this problem revolve around the concept of set differences. Given two integer arrays, we need to find the unique inte
# Intuition\n\nOur first thoughts on solving this problem revolve around the concept of set differences. Given two integer arrays, we need to find the unique integers in the first array that are not in the second, and vice versa. The idea of set difference fits perfectly here. \n\nhttps://youtu.be/eo-NwnOjF1Q\n\n# Approach\n\nWe approach this problem by leveraging the capabilities of Python sets. Here are the steps:\n\n1. Convert the input lists to sets. This serves two purposes: it removes any duplicate elements, and it allows us to perform set operations. \n\n2. Use the `-` operator to find the difference between the sets. This gives us the elements that are in one set but not in the other.\n\n3. Convert the resulting sets back to lists and return them.\n\n# Complexity\n\n- Time complexity: The time complexity of our solution is \\(O(n)\\), where \\(n\\) is the length of the longer input list. This is because we iterate through each list once to convert it to a set.\n\n- Space complexity: The space complexity of our solution is also \\(O(n)\\), where \\(n\\) is the length of the longer input list. This is because we create a new set for each input list.\n\nThis Python code defines a function `findDifference` that finds and returns the difference between two input arrays. The function is part of a class `Solution`, as required by the LeetCode problem format.\n\n# Code\n\n```Python []\nfrom typing import List\n\nclass Solution:\n    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n        set1 = set(nums1)\n        set2 = set(nums2)\n        diff1 = set1 - set2\n        diff2 = set2 - set1\n        return [list(diff1), list(diff2)]\n```\n``` C++ []\nclass Solution {\npublic:\n    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {\n        set<int> set1(nums1.begin(), nums1.end());\n        set<int> set2(nums2.begin(), nums2.end());\n        \n        vector<int> diff1, diff2;\n        for(int num : set1)\n            if(set2.find(num) == set2.end())\n                diff1.push_back(num);\n                \n        for(int num : set2)\n            if(set1.find(num) == set1.end())\n                diff2.push_back(num);\n                \n        return {diff1, diff2};\n    }\n};\n```\n``` JavaScript []\n/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number[][]}\n */\nvar findDifference = function(nums1, nums2) {\n        let set1 = new Set(nums1);\n        let set2 = new Set(nums2);\n\n        let diff1 = [...set1].filter(x => !set2.has(x));\n        let diff2 = [...set2].filter(x => !set1.has(x));\n\n        return [diff1, diff2];\n};\n```\n``` C# []\npublic class Solution {\n    public IList<IList<int>> FindDifference(int[] nums1, int[] nums2) {\n        HashSet<int> set1 = new HashSet<int>(nums1);\n        HashSet<int> set2 = new HashSet<int>(nums2);\n        \n        set1.ExceptWith(nums2);\n        set2.ExceptWith(nums1);\n        \n        return new List<IList<int>> { set1.ToList(), set2.ToList() };\n    }\n}\n```\n``` Java []\nclass Solution {\n    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {\n        Set<Integer> set1 = Arrays.stream(nums1).boxed().collect(Collectors.toSet());\n        Set<Integer> set2 = Arrays.stream(nums2).boxed().collect(Collectors.toSet());\n        \n        // Remove all elements from set1 that are present in set2\n        set1.removeAll(Arrays.stream(nums2).boxed().collect(Collectors.toSet()));\n        \n        // Remove all elements from set2 that are present in set1\n        set2.removeAll(Arrays.stream(nums1).boxed().collect(Collectors.toSet()));\n        \n        return Arrays.asList(new ArrayList<>(set1), new ArrayList<>(set2));\n    }\n}\n```
['C++', 'Java', 'Python3', 'JavaScript', 'C#']