Skip to content

Commit e9446b3

Browse files
authored
feat: update solutions to lc problem: No.2206 (#4262)
1 parent 1c7f7c0 commit e9446b3

File tree

4 files changed

+74
-38
lines changed

4 files changed

+74
-38
lines changed

solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md

+27-15
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,6 @@ func divideArray(nums []int) bool {
144144
}
145145
```
146146

147-
#### TypeScript
148-
149-
```ts
150-
function divideArray(nums: number[]): boolean {
151-
const cnt: Record<number, number> = {};
152-
for (const x of nums) {
153-
cnt[x] = (cnt[x] || 0) + 1;
154-
}
155-
return Object.values(cnt).every(x => x % 2 === 0);
156-
}
157-
```
158-
159147
#### Rust
160148

161149
```rust
@@ -172,6 +160,24 @@ impl Solution {
172160
}
173161
```
174162

163+
#### TypeScript
164+
165+
```ts
166+
function divideArray(nums: number[]): boolean {
167+
const cnt = Array(501).fill(0);
168+
169+
for (const x of nums) {
170+
cnt[x]++;
171+
}
172+
173+
for (const x of cnt) {
174+
if (x & 1) return false;
175+
}
176+
177+
return true;
178+
}
179+
```
180+
175181
#### JavaScript
176182

177183
```js
@@ -180,11 +186,17 @@ impl Solution {
180186
* @return {boolean}
181187
*/
182188
var divideArray = function (nums) {
183-
const cnt = {};
189+
const cnt = Array(501).fill(0);
190+
184191
for (const x of nums) {
185-
cnt[x] = (cnt[x] || 0) + 1;
192+
cnt[x]++;
186193
}
187-
return Object.values(cnt).every(x => x % 2 === 0);
194+
195+
for (const x of cnt) {
196+
if (x & 1) return false;
197+
}
198+
199+
return true;
188200
};
189201
```
190202

solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md

+29-17
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tags:
3838
<pre>
3939
<strong>Input:</strong> nums = [3,2,3,2,2,2]
4040
<strong>Output:</strong> true
41-
<strong>Explanation:</strong>
41+
<strong>Explanation:</strong>
4242
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
4343
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
4444
</pre>
@@ -48,7 +48,7 @@ If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy al
4848
<pre>
4949
<strong>Input:</strong> nums = [1,2,3,4]
5050
<strong>Output:</strong> false
51-
<strong>Explanation:</strong>
51+
<strong>Explanation:</strong>
5252
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
5353
</pre>
5454

@@ -142,18 +142,6 @@ func divideArray(nums []int) bool {
142142
}
143143
```
144144

145-
#### TypeScript
146-
147-
```ts
148-
function divideArray(nums: number[]): boolean {
149-
const cnt: Record<number, number> = {};
150-
for (const x of nums) {
151-
cnt[x] = (cnt[x] || 0) + 1;
152-
}
153-
return Object.values(cnt).every(x => x % 2 === 0);
154-
}
155-
```
156-
157145
#### Rust
158146

159147
```rust
@@ -170,6 +158,24 @@ impl Solution {
170158
}
171159
```
172160

161+
#### TypeScript
162+
163+
```ts
164+
function divideArray(nums: number[]): boolean {
165+
const cnt = Array(501).fill(0);
166+
167+
for (const x of nums) {
168+
cnt[x]++;
169+
}
170+
171+
for (const x of cnt) {
172+
if (x & 1) return false;
173+
}
174+
175+
return true;
176+
}
177+
```
178+
173179
#### JavaScript
174180

175181
```js
@@ -178,11 +184,17 @@ impl Solution {
178184
* @return {boolean}
179185
*/
180186
var divideArray = function (nums) {
181-
const cnt = {};
187+
const cnt = Array(501).fill(0);
188+
182189
for (const x of nums) {
183-
cnt[x] = (cnt[x] || 0) + 1;
190+
cnt[x]++;
184191
}
185-
return Object.values(cnt).every(x => x % 2 === 0);
192+
193+
for (const x of cnt) {
194+
if (x & 1) return false;
195+
}
196+
197+
return true;
186198
};
187199
```
188200

solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
* @return {boolean}
44
*/
55
var divideArray = function (nums) {
6-
const cnt = {};
6+
const cnt = Array(501).fill(0);
7+
78
for (const x of nums) {
8-
cnt[x] = (cnt[x] || 0) + 1;
9+
cnt[x]++;
910
}
10-
return Object.values(cnt).every(x => x % 2 === 0);
11+
12+
for (const x of cnt) {
13+
if (x & 1) return false;
14+
}
15+
16+
return true;
1117
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
function divideArray(nums: number[]): boolean {
2-
const cnt: Record<number, number> = {};
2+
const cnt = Array(501).fill(0);
3+
34
for (const x of nums) {
4-
cnt[x] = (cnt[x] || 0) + 1;
5+
cnt[x]++;
56
}
6-
return Object.values(cnt).every(x => x % 2 === 0);
7+
8+
for (const x of cnt) {
9+
if (x & 1) return false;
10+
}
11+
12+
return true;
713
}

0 commit comments

Comments
 (0)