✅ 97% beats || Python3 || Set
\n\n\n\n# Intuition\nTo solve this problem, we can use a greedy approach. We aim to maximize the size of the set s after removing elements from nums1 and nums2.
![image.png](https://assets.leetcode.com/users/images/dcce0479-c249-46fe-95b7-d9174f480bdc_1716727079.4363635.png)\n\n\n\n# Intuition\nTo solve this problem, we can use a greedy approach. We aim to maximize the size of the set `s` after removing elements from `nums1` and `nums2`. We can achieve this by removing elements that appear the most in both arrays. Additionally, we may need to remove elements from one array to balance the removals between the two arrays.\n\n# Approach\n1. Initialize variables `length`, `n1`, and `n2`. `length` represents half the length of the input arrays, while `n1` and `n2` are sets of unique elements from `nums1` and `nums2`, respectively.\n2. Calculate the number of elements common to both `n1` and `n2` and store it in `inter_num`.\n3. Calculate the difference between the lengths of `n1` and `n2` and `length`, representing the excess elements in each array.\n4. Determine the maximum possible difference between the lengths of `n1` and `n2` and `length`, or the number of elements common to both arrays. Store this maximum difference in `max_diff`.\n5. Return the sum of the lengths of `n1` and `n2` minus `max_diff`, which represents the maximum possible size of the set `s`.\n\n# Complexity\n- Time complexity: O(n), where `n` is the total number of elements in both `nums1` and `nums2`. Calculating the set intersection and differences takes linear time.\n- Space complexity: O(n), where `n` is the total number of elements in both `nums1` and `nums2`. The space complexity is dominated by the sets `n1` and `n2`, which store unique elements from the input arrays.\n\n\n# Code\n```\nclass Solution:\n    def maximumSetSize(self, nums1: list[int], nums2: list[int]) -> int:\n        length, n1, n2 = len(nums1) // 2, set(nums1), set(nums2)\n        inter_num = len(n1.intersection(n2))\n        diff = 0\n\n        diff += len(n1) - length if len(n1) >= length else 0\n        diff += len(n2) - length if len(n2) >= length else 0\n\n        max_diff=max(diff,inter_num)\n\n        return (len(n1)+len(n2))-max_diff\n\n\n\n\n```\n![cat_upvote.gif](https://assets.leetcode.com/users/images/75d21a32-f2a1-485a-8cb1-a53bcc353be3_1716726887.2945015.gif)\n\n\n
['Python3']