Easy solution using map in c++
Before moving to the solution let see why we have to use the map for this question.\nWe have to find the element that is present in the first array and the same
Before moving to the solution let see why we have to use the map for this question.\nWe have to find the element that is present in the first array and the same is not present in the second array.\nSimilarly for the second array also.\nWe can find this element using O(1) time complexity using map.\nNow the question came why we have to iterate the map and not the elements of the array while searching.\nWhen we iterate the array then there are situation in which we got the element that are present in the array more than once and hence it will be pushed more than once in the temp data structure.\nIn order to tackle this situation we will iterate the map rather than arrays.\n```\nclass Solution {\npublic:\n    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {\n        vector<vector<int>> ans;\n        unordered_map<int,int> mp1,mp2;\n        for(auto it : nums1)    mp1[it]++;\n        for(auto it : nums2)    mp2[it]++;\n        vector<int> temp;\n        for(auto it : mp1){\n            if(mp2.find(it.first) == mp2.end())\n                temp.push_back(it.first);\n        }\n        ans.push_back(temp);\n        temp.clear();\n        for(auto it : mp2){\n            if(mp1.find(it.first) == mp1.end())\n                temp.push_back(it.first);\n        }\n        ans.push_back(temp);\n        return ans;\n    }\n};\n```\nIf you like this approach Please Upvote it.\nFeel free to ask any doubt
[]