diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md index 69f019780fa1d..d24acdbd92927 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README.md @@ -144,18 +144,6 @@ func divideArray(nums []int) bool { } ``` -#### TypeScript - -```ts -function divideArray(nums: number[]): boolean { - const cnt: Record = {}; - for (const x of nums) { - cnt[x] = (cnt[x] || 0) + 1; - } - return Object.values(cnt).every(x => x % 2 === 0); -} -``` - #### Rust ```rust @@ -172,6 +160,24 @@ impl Solution { } ``` +#### TypeScript + +```ts +function divideArray(nums: number[]): boolean { + const cnt = Array(501).fill(0); + + for (const x of nums) { + cnt[x]++; + } + + for (const x of cnt) { + if (x & 1) return false; + } + + return true; +} +``` + #### JavaScript ```js @@ -180,11 +186,17 @@ impl Solution { * @return {boolean} */ var divideArray = function (nums) { - const cnt = {}; + const cnt = Array(501).fill(0); + for (const x of nums) { - cnt[x] = (cnt[x] || 0) + 1; + cnt[x]++; } - return Object.values(cnt).every(x => x % 2 === 0); + + for (const x of cnt) { + if (x & 1) return false; + } + + return true; }; ``` diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md index 7e4e94cfccf86..fc1ded8217a77 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/README_EN.md @@ -38,7 +38,7 @@ tags:
 Input: nums = [3,2,3,2,2,2]
 Output: true
-Explanation: 
+Explanation:
 There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
 If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
 
@@ -48,7 +48,7 @@ If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy al
 Input: nums = [1,2,3,4]
 Output: false
-Explanation: 
+Explanation:
 There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
 
@@ -142,18 +142,6 @@ func divideArray(nums []int) bool { } ``` -#### TypeScript - -```ts -function divideArray(nums: number[]): boolean { - const cnt: Record = {}; - for (const x of nums) { - cnt[x] = (cnt[x] || 0) + 1; - } - return Object.values(cnt).every(x => x % 2 === 0); -} -``` - #### Rust ```rust @@ -170,6 +158,24 @@ impl Solution { } ``` +#### TypeScript + +```ts +function divideArray(nums: number[]): boolean { + const cnt = Array(501).fill(0); + + for (const x of nums) { + cnt[x]++; + } + + for (const x of cnt) { + if (x & 1) return false; + } + + return true; +} +``` + #### JavaScript ```js @@ -178,11 +184,17 @@ impl Solution { * @return {boolean} */ var divideArray = function (nums) { - const cnt = {}; + const cnt = Array(501).fill(0); + for (const x of nums) { - cnt[x] = (cnt[x] || 0) + 1; + cnt[x]++; } - return Object.values(cnt).every(x => x % 2 === 0); + + for (const x of cnt) { + if (x & 1) return false; + } + + return true; }; ``` diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js index 6ec8af3eaa8a8..987dc8c17d6fd 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.js @@ -3,9 +3,15 @@ * @return {boolean} */ var divideArray = function (nums) { - const cnt = {}; + const cnt = Array(501).fill(0); + for (const x of nums) { - cnt[x] = (cnt[x] || 0) + 1; + cnt[x]++; } - return Object.values(cnt).every(x => x % 2 === 0); + + for (const x of cnt) { + if (x & 1) return false; + } + + return true; }; diff --git a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts index a6604adfe2ae3..7f8606168a301 100644 --- a/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts +++ b/solution/2200-2299/2206.Divide Array Into Equal Pairs/Solution.ts @@ -1,7 +1,13 @@ function divideArray(nums: number[]): boolean { - const cnt: Record = {}; + const cnt = Array(501).fill(0); + for (const x of nums) { - cnt[x] = (cnt[x] || 0) + 1; + cnt[x]++; } - return Object.values(cnt).every(x => x % 2 === 0); + + for (const x of cnt) { + if (x & 1) return false; + } + + return true; }