Easy Solution | Hashing | Better Approach
Intuition\nThe goal is to find elements that are unique to each of the two arrays. Using HashSet provides an efficient way to handle duplicates and perform memb
# Intuition\nThe goal is to find elements that are unique to each of the two arrays. Using `HashSet` provides an efficient way to handle duplicates and perform membership checks in constant average time complexity.\n\n- The problem boils down to finding elements that are **only in `nums1`** and elements that are **only in `nums2`**.\n- By leveraging `HashSet`, we can efficiently find these differences while avoiding duplicates in the result.\n\n---\n\n# Approach\n1. **Convert Arrays to HashSets**:\n   - Add all elements of `nums1` to `h1` and all elements of `nums2` to `h2`.\n   - This removes any duplicates within each array.\n\n2. **Identify Common Elements**:\n   - Iterate through one of the arrays (`nums2`) and check if the element exists in `h1`.\n   - If an element is found in both sets, remove it from both `h1` and `h2` to ensure it\'s excluded from the final result.\n\n3. **Build the Result**:\n   - The remaining elements in `h1` are unique to `nums1`, and the remaining elements in `h2` are unique to `nums2`.\n   - Convert these sets to lists and return them as part of a nested list.\n\n---\n\n# Complexity\n\n- **Time Complexity**:  \n  $$O(n + m)$$  \n  - \\(O(n)\\) to populate `h1` with elements from `nums1`.  \n  - \\(O(m)\\) to populate `h2` with elements from `nums2`.  \n  - Iterating through `nums2` and performing membership checks/removals is \\(O(m)\\) on average.  \n\n- **Space Complexity**:  \n  $$O(n + m)$$  \n  - Storage for `h1` and `h2` to hold all unique elements from `nums1` and `nums2`.\n\n---\n\n# Code\n\n### Java\n```java\nimport java.util.*;\n\nclass Solution {\n    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {\n        // Initialize sets for both arrays\n        HashSet<Integer> h1 = new HashSet<>();\n        HashSet<Integer> h2 = new HashSet<>();\n        \n        // Populate the sets\n        for (int n : nums1) h1.add(n);\n        for (int n : nums2) h2.add(n);\n\n        // Remove common elements from both sets\n        for (int n : nums2) {\n            if (h1.contains(n)) {\n                h1.remove(n);\n                h2.remove(n);\n            }\n        }\n\n        // Build the result\n        List<List<Integer>> result = new ArrayList<>();\n        result.add(new ArrayList<>(h1)); // Unique to nums1\n        result.add(new ArrayList<>(h2)); // Unique to nums2\n        \n        return result;\n    }\n}\n```\n\n### Python\n```python\nclass Solution:\n    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n        # Convert arrays to sets\n        h1 = set(nums1)\n        h2 = set(nums2)\n\n        # Remove common elements\n        for num in nums2:\n            if num in h1:\n                h1.remove(num)\n                h2.discard(num)\n\n        # Return the remaining unique elements\n        return [list(h1), list(h2)]\n\n```\n### CPP\n```cpp\nclass Solution {\npublic:\n    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {\n        // Use unordered_set to store unique elements\n        unordered_set<int> h1(nums1.begin(), nums1.end());\n        unordered_set<int> h2(nums2.begin(), nums2.end());\n\n        // Remove common elements\n        for (int num : nums2) {\n            if (h1.find(num) != h1.end()) {\n                h1.erase(num);\n                h2.erase(num);\n            }\n        }\n\n        // Build and return the result\n        return {vector<int>(h1.begin(), h1.end()), vector<int>(h2.begin(), h2.end())};\n    }\n};\n\n```\n\n### Javascript\n```js\nvar findDifference = function(nums1, nums2) {\n    // Convert arrays to sets\n    const h1 = new Set(nums1);\n    const h2 = new Set(nums2);\n\n    // Remove common elements\n    for (let num of nums2) {\n        if (h1.has(num)) {\n            h1.delete(num);\n            h2.delete(num);\n        }\n    }\n\n    // Convert sets to arrays and return\n    return [Array.from(h1), Array.from(h2)];\n};\n```\n
['Array', 'Hash Table', 'C++', 'Java', 'Python3', 'JavaScript']