Java HashSet O(N)
See comments for details\n\n- Time complexity:\n Add your time complexity here, e.g. O(n) \nO(N)\n- Space complexity:\n Add your space complexity here, e.g. O(n
See comments for details\n\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n# Code\n```\nclass Solution {\n    public int maximumSetSize(int[] nums1, int[] nums2) {\n        Set<Integer> set1 = toSet(nums1), set2 = toSet(nums2);\n        int common = 0, m1 = set1.size(), m2 = set2.size(), n = nums1.length;\n        for (int num : set1) {\n            if (set2.contains(num)) common++;\n        }\n        int uniq1 = m1 - common, uniq2 = m2 - common;\n        // uniq is enough in both sets;\n        if (uniq1 >= n / 2 && uniq2 >= n / 2) return n;\n        // uniq is not enough in either sets;\n        // try to divide common in set 1 or set 2, greedy;\n        if (uniq1 < n / 2 && uniq2 < n / 2) return Math.min(n, uniq1 + uniq2 + common);\n        // if only set 2 has enough unique\n        // put all common in set 1;\n        if (uniq1 < n / 2) return Math.min(n / 2, uniq1 + common) + n/2;\n        // if only set 1 has enough unique\n        // put all common in set 2;\n        return Math.min(n / 2, uniq2 + common) + n/2;\n    }\n\n    private Set<Integer> toSet(int[] arr) {\n        Set<Integer> set = new HashSet();\n        for (int num : arr) set.add(num);\n        return set;\n    }\n}\n```
['Java']