Simple Java Set Solution: Beats 89%
Intuition\n Describe your first thoughts on how to solve this problem. \nIn order to maximize set size, you want to remove duplicates first before having to rem
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn order to maximize set size, you want to remove duplicates first before having to remove numbers that only appear once.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCalculate how many numbers you need to remove from both arrays. Store all seen numbers in a set. So, if the set contains a number, that means it\'s a duplicate and can reduce the amount of numbers to remove from the respective array (n1R or n2R).\n\nLogic behind Math.max() in return statement:\n If there are more shared values between the two array sets than there are elements needed to be removed from each set (ie same > n1R + n2R), then the total set will be limited by how many shared values there are. \n\nEx: nums1 set {1, 2, 3} with n1R = 1 (original size 3) and nums2 set {2, 3, 4} (original size 3) with n2R = 0. There are 2 values that are the same and only 1 element that needs to be removed. So, you can take care of the last element needed to be removed from nums1 by removing one of the shared values, like 2, creating a set of {1, 3}. But, because nums1 set and nums2 set still share the value of 3, the combined set will be {1, 3, 4}. This set is the same size of 3 + 3 - Math.max(2, 1 + 0)\nWhich is the equation n1.size() + n2.size() - Math.max(same, n1R + n2R)\n\nThe same logic can be applied with the opposite situtation, where there are more elements needed to be removed than shared values\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nums1.length + nums2.length)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(nums1.length + nums2.length)\n\n# Code\n```\nclass Solution {\n    public int maximumSetSize(int[] nums1, int[] nums2) {\n        Set<Integer> n1 = new HashSet<>();\n        int n1R = nums1.length / 2;\n\n        Set<Integer> n2 = new HashSet<>();\n        int n2R = nums2.length / 2;\n\n        for (int i : nums1) {\n            if (n1.contains(i)) {\n                if (n1R > 0) {\n                    n1R--;\n                }    \n            } else {\n                n1.add(i);\n            }\n        }\n\n        for (int i : nums2) {\n            if (n2.contains(i)) {\n                if (n2R > 0) {\n                    n2R--;\n                }\n            } else {\n                n2.add(i);\n            }\n        }\n\n        int same = 0;\n        for (int i : n1) {\n            if (n2.contains(i)) {\n                same++;\n            }\n        }\n\n        return n1.size() + n2.size() - Math.max(same, n1R + n2R);\n    }\n}\n```
['Java']