💯Efficient Set Approach for Finding Difference in Two Arrays
Intuition\n- Imagine nums1 and nums2 as two sets of marbles. You want to find the marbles that are on only one table, not on both.\n-  The unordered sets act li
# Intuition\n- Imagine nums1 and nums2 as two sets of marbles. You want to find the marbles that are on only one table, not on both.\n-  The unordered sets act like hash tables, allowing for quick checks to see if a specific marble exists in the other set. \n- We iterate through each marble in one set, checking if it\'s present in the other set. If not, it\'s unique and gets added to the result.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) ***Populate Unordered Sets:*** \n    - We insert the elements from nums1 and nums2 into s1 and s2 respectively, using their iterators.\n2) ***Find Elements Unique to nums1:***\n    - We iterate through each element (num) in nums1.\n    - For each num:\n        - We use find(num) in s2 to check if num exists in the other set (nums2).\n        - If find(num) == s2.end() (meaning num is not found in s2), it\'s unique to nums1. We add it to the first sub-vector in ans.\n3) ***Find Elements Unique to nums2:***\n    - We repeat the process for nums2. We iterate through each element (num) and check if it exists in s1. If it doesn\'t, it\'s unique to nums2 and is added to the second sub-vector in ans.\n\n# Complexity\n- Time complexity: ***O(n + m)***\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: ***O(n + m)***\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {\n        set<int> s1,s2;\n        vector<vector<int>> ans(2);\n        for(auto i : nums1){\n            s1.insert(i);\n        }\n        for(auto i : nums2){\n            s2.insert(i);\n        }\n        for(auto i : s1){\n            if(s2.find(i) == s2.end()){\n                ans[0].push_back(i);\n            }\n        }\n        for(auto i : s2){\n            if(s1.find(i) == s1.end()){\n                ans[1].push_back(i);\n            }\n        }\n        return ans;\n    }\n};\n```\n```python []\nclass Solution:\n    def findDifference(self, nums1, nums2):\n        s1, s2 = set(nums1), set(nums2)\n        ans = [[], []]\n\n        for i in s1:\n            if i not in s2:\n                ans[0].append(i)\n        \n        for i in s2:\n            if i not in s1:\n                ans[1].append(i)\n        \n        return ans\n\n```\n```java []\nimport java.util.*;\n\nclass Solution {\n    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {\n        Set<Integer> s1 = new HashSet<>();\n        Set<Integer> s2 = new HashSet<>();\n        List<List<Integer>> ans = new ArrayList<>();\n        ans.add(new ArrayList<>());\n        ans.add(new ArrayList<>());\n\n        for (int i : nums1) {\n            s1.add(i);\n        }\n        \n        for (int i : nums2) {\n            s2.add(i);\n        }\n\n        for (int i : s1) {\n            if (!s2.contains(i)) {\n                ans.get(0).add(i);\n            }\n        }\n\n        for (int i : s2) {\n            if (!s1.contains(i)) {\n                ans.get(1).add(i);\n            }\n        }\n\n        return ans;\n    }\n}\n\n```\n```javascript []\nvar findDifference = function(nums1, nums2) {\n    let s1 = new Set(nums1);\n    let s2 = new Set(nums2);\n    let ans = [[], []];\n\n    for (let i of s1) {\n        if (!s2.has(i)) {\n            ans[0].push(i);\n        }\n    }\n\n    for (let i of s2) {\n        if (!s1.has(i)) {\n            ans[1].push(i);\n        }\n    }\n\n    return ans;\n};\n\n```\nPLEASE UPVOTE !!! \uD83D\uDE4F\n\n![Gemini_Generated_Image_3dhm323dhm323dhm.jpg](https://assets.leetcode.com/users/images/81cfcf90-7ae0-4b9a-807b-8d59aac17ebd_1720511129.4538867.jpeg)\n
['Hash Table', 'Ordered Set', 'C++', 'Java', 'Python3', 'JavaScript']