Greedy | Beats 81% | Using 2 Hash Tables
Submission Screenshot\n\n\n\n\n# Code\ncpp\nclass Solution {\npublic:\n    int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n        unordered_map<i
# Submission Screenshot\n\n![image.png](https://assets.leetcode.com/users/images/6412696a-122d-4899-9f07-74160809a621_1709963757.577999.png)\n\n\n# Code\n```cpp\nclass Solution {\npublic:\n    int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {\n        unordered_map<int, int> mp1;\n        unordered_map<int, int> mp2;\n        // Make frequency maps for both arrays.\n        for(int i=0; i<nums1.size(); i++) mp1[nums1[i]]++;\n        for(int i=0; i<nums2.size(); i++) mp2[nums2[i]]++;\n        int toDelete1 = nums1.size()/2, toDelete2 = nums2.size()/2;\n        // Remove as many duplicates as possible\n        for(auto i=mp1.begin(); i!=mp1.end(); i++) {\n            if(i->second > 1) {\n                toDelete1 -= (i->second - 1);\n                i->second = 1;\n            }\n        }\n        for(auto i=mp2.begin(); i!=mp2.end(); i++) {\n            if(i->second > 1) {\n                toDelete2 -= (i->second - 1);\n                i->second = 1;\n            }\n        }\n        // If removing duplicates satisfies the number of removals expected than return\n        if(toDelete1<=0 && toDelete2<=0){\n            mp1.insert(mp2.begin(), mp2.end());\n            return mp1.size();\n        }\n        // If not, merge the maps. And again do the same thing i.e, remove duplicates.\n        for(auto i=mp2.begin(); i!=mp2.end(); i++) mp1[i->first]++;\n        if(toDelete1<=0 || toDelete2<=0) toDelete1 = max(toDelete1, toDelete2);\n        else toDelete1 = toDelete1 + toDelete2;\n        for(auto i=mp1.begin(); i!=mp1.end(); i++) {\n            if(i->second > 1) {\n                toDelete1 = toDelete1 - 1;\n                i->second = 1;\n            }\n        }\n        // If removing duplicates from mergind, satisfies the removal condition then return\n        if(toDelete1<=0) return mp1.size();\n        // Else,...\n        return mp1.size()-toDelete1;\n    }\n};\n```
['Array', 'Hash Table', 'Greedy', 'C++']