JavaScript Greedy O(n)
Intuition\nUse the greedy approach. Of course, we should remove duplicates first.\n# Approach\nFor each set:\n  1. Remove all local duplicate elements until hal
# Intuition\nUse the greedy approach. Of course, we should remove duplicates first.\n# Approach\nFor each set:\n  1. Remove all local duplicate elements until half are left\n  2. If there are more than half left, remove elements that are also in the other set, until half are left\n  3. If there are still more than half left, remove any elements until half are left\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(n)\n# Code\n```\n/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar maximumSetSize = function(nums1, nums2) {\n    const len = nums1.length, half = len >> 1\n    function toFreqs(nums) {\n        const map = new Map()\n        for (const val of nums)\n            map.set(val, 1 + (map.get(val) ?? 0))\n\n        return map\n    }\n\n    function removeDuplicates(freqs, limit) {\n        let result = 0\n        for (const [val, freq] of freqs.entries()) {\n            const needToRemove = freq - 1\n            if (needToRemove > 0) {\n                if (limit > needToRemove) {\n                    limit -= needToRemove\n                    freqs.set(val, 1)\n                    result += needToRemove\n                } else {\n                    freqs.set(val, freq - limit)\n                    result += limit\n                    break\n                }\n            }\n        }\n\n        return result\n    }\n\n\n    const freqsA = toFreqs(nums1), freqsB = toFreqs(nums2)\n    let rmgA = half - removeDuplicates(freqsA, half)\n    let rmgB = half - removeDuplicates(freqsB, half)\n\n    if (rmgA > 0) {\n        for (const val of freqsA.keys()) {\n            if (freqsB.get(val) > 0) {\n                freqsA.set(val, 0)\n                rmgA--\n                if (rmgA === 0)\n                    break\n            }\n        }\n    }\n    if (rmgB > 0) {\n        for (const val of freqsB.keys()) {\n            if (freqsA.get(val) > 0) {\n                freqsB.set(val, 0)\n                rmgB--\n                if (rmgB === 0)\n                    break\n            }\n        }\n    }\n\n\n    if (rmgA > 0) {\n        for (const [val, freq] of freqsA.entries()) {\n            if (freq !== 0) {\n                freqsA.set(val, 0)\n                rmgA--\n                if (rmgA === 0)\n                    break\n            }\n        }\n    }\n\n    if (rmgB > 0) {\n        for (const [val, freq] of freqsB.entries()) {\n            if (freq !== 0) {\n                freqsB.set(val, 0)\n                rmgB--\n                if (rmgB === 0)\n                    break\n            }\n        }\n    }\n\n\n    const set = new Set()\n    for (const [val, freq] of freqsA) {\n        if (freq)   set.add(val)\n    }\n    for (const [val, freq] of freqsB) {\n        if (freq)   set.add(val)\n    }\n\n\n    return set.size\n};\n```
['JavaScript']