@@ -55,25 +55,135 @@ It can be shown that 6 is the maximum possible size of the set s after the remov
55
55
### ** Python3**
56
56
57
57
``` python
58
-
58
+ class Solution :
59
+ def maximumSetSize (self , nums1 : List[int ], nums2 : List[int ]) -> int :
60
+ s1 = set (nums1)
61
+ s2 = set (nums2)
62
+ n = len (nums1)
63
+ a = min (len (s1 - s2), n // 2 )
64
+ b = min (len (s2 - s1), n // 2 )
65
+ return min (a + b + len (s1 & s2), n)
59
66
```
60
67
61
68
### ** Java**
62
69
63
70
``` java
64
-
71
+ class Solution {
72
+ public int maximumSetSize (int [] nums1 , int [] nums2 ) {
73
+ Set<Integer > s1 = new HashSet<> ();
74
+ Set<Integer > s2 = new HashSet<> ();
75
+ for (int x : nums1) {
76
+ s1. add(x);
77
+ }
78
+ for (int x : nums2) {
79
+ s2. add(x);
80
+ }
81
+ int n = nums1. length;
82
+ int a = 0 , b = 0 , c = 0 ;
83
+ for (int x : s1) {
84
+ if (! s2. contains(x)) {
85
+ ++ a;
86
+ }
87
+ }
88
+ for (int x : s2) {
89
+ if (! s1. contains(x)) {
90
+ ++ b;
91
+ } else {
92
+ ++ c;
93
+ }
94
+ }
95
+ a = Math . min(a, n / 2 );
96
+ b = Math . min(b, n / 2 );
97
+ return Math . min(a + b + c, n);
98
+ }
99
+ }
65
100
```
66
101
67
102
### ** C++**
68
103
69
104
``` cpp
70
-
105
+ class Solution {
106
+ public:
107
+ int maximumSetSize(vector<int >& nums1, vector<int >& nums2) {
108
+ unordered_set<int > s1(nums1.begin(), nums1.end());
109
+ unordered_set<int > s2(nums2.begin(), nums2.end());
110
+ int n = nums1.size();
111
+ int a = 0, b = 0, c = 0;
112
+ for (int x : s1) {
113
+ if (!s2.count(x)) {
114
+ ++a;
115
+ }
116
+ }
117
+ for (int x : s2) {
118
+ if (!s1.count(x)) {
119
+ ++b;
120
+ } else {
121
+ ++c;
122
+ }
123
+ }
124
+ a = min(a, n / 2);
125
+ b = min(b, n / 2);
126
+ return min(a + b + c, n);
127
+ }
128
+ };
71
129
```
72
130
73
131
### **Go**
74
132
75
133
```go
134
+ func maximumSetSize(nums1 []int, nums2 []int) int {
135
+ s1 := map[int]bool{}
136
+ s2 := map[int]bool{}
137
+ for _, x := range nums1 {
138
+ s1[x] = true
139
+ }
140
+ for _, x := range nums2 {
141
+ s2[x] = true
142
+ }
143
+ a, b, c := 0, 0, 0
144
+ for x := range s1 {
145
+ if !s2[x] {
146
+ a++
147
+ }
148
+ }
149
+ for x := range s2 {
150
+ if !s1[x] {
151
+ b++
152
+ } else {
153
+ c++
154
+ }
155
+ }
156
+ n := len(nums1)
157
+ a = min(a, n/2)
158
+ b = min(b, n/2)
159
+ return min(a+b+c, n)
160
+ }
161
+ ```
76
162
163
+ ### ** TypeScript**
164
+
165
+ ``` ts
166
+ function maximumSetSize(nums1 : number [], nums2 : number []): number {
167
+ const s1: Set <number > = new Set (nums1 );
168
+ const s2: Set <number > = new Set (nums2 );
169
+ const n = nums1 .length ;
170
+ let [a, b, c] = [0 , 0 , 0 ];
171
+ for (const x of s1 ) {
172
+ if (! s2 .has (x )) {
173
+ ++ a ;
174
+ }
175
+ }
176
+ for (const x of s2 ) {
177
+ if (! s1 .has (x )) {
178
+ ++ b ;
179
+ } else {
180
+ ++ c ;
181
+ }
182
+ }
183
+ a = Math .min (a , n >> 1 );
184
+ b = Math .min (b , n >> 1 );
185
+ return Math .min (a + b + c , n );
186
+ }
77
187
```
78
188
79
189
### ** ...**
0 commit comments