Skip to content

Commit f43e6c5

Browse files
committed
feat: add solutions to lc problem: No.2341
No.2341.Maximum Number of Pairs in Array
1 parent ea8bbb9 commit f43e6c5

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

solution/2300-2399/2341.Maximum Number of Pairs in Array/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,58 @@ func numberOfPairs(nums []int) []int {
127127
### **TypeScript**
128128

129129
```ts
130+
function numberOfPairs(nums: number[]): number[] {
131+
const n = nums.length;
132+
const count = new Array(101).fill(0);
133+
for (const num of nums) {
134+
count[num]++;
135+
}
136+
const sum = count.reduce((r, v) => r + (v >> 1), 0);
137+
return [sum, n - sum * 2];
138+
}
139+
140+
```
130141

142+
### **Rust**
143+
144+
```rust
145+
impl Solution {
146+
pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> {
147+
let n = nums.len();
148+
let mut count = [0; 101];
149+
for &v in nums.iter() {
150+
count[v as usize] += 1;
151+
}
152+
let mut sum = 0;
153+
for v in count.iter() {
154+
sum += v >> 1;
155+
}
156+
vec![sum as i32, (n - sum * 2) as i32]
157+
}
158+
}
159+
```
160+
161+
### **C**
162+
163+
```c
164+
/**
165+
* Note: The returned array must be malloced, assume caller calls free().
166+
*/
167+
int *numberOfPairs(int *nums, int numsSize, int *returnSize) {
168+
int count[101] = {0};
169+
for (int i = 0; i < numsSize; i++) {
170+
count[nums[i]]++;
171+
}
172+
int sum = 0;
173+
for (int i = 0; i < 101; i++) {
174+
sum += count[i] >> 1;
175+
}
176+
int *ans = malloc(sizeof(int) * 2);
177+
ans[0] = sum;
178+
ans[1] = numsSize - sum * 2;
179+
*returnSize = 2;
180+
return ans;
181+
}
131182
```
132183
133184
### **...**

solution/2300-2399/2341.Maximum Number of Pairs in Array/README_EN.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,58 @@ func numberOfPairs(nums []int) []int {
122122
### **TypeScript**
123123

124124
```ts
125+
function numberOfPairs(nums: number[]): number[] {
126+
const n = nums.length;
127+
const count = new Array(101).fill(0);
128+
for (const num of nums) {
129+
count[num]++;
130+
}
131+
const sum = count.reduce((r, v) => r + (v >> 1), 0);
132+
return [sum, n - sum * 2];
133+
}
134+
135+
```
125136

137+
### **Rust**
138+
139+
```rust
140+
impl Solution {
141+
pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> {
142+
let n = nums.len();
143+
let mut count = [0; 101];
144+
for &v in nums.iter() {
145+
count[v as usize] += 1;
146+
}
147+
let mut sum = 0;
148+
for v in count.iter() {
149+
sum += v >> 1;
150+
}
151+
vec![sum as i32, (n - sum * 2) as i32]
152+
}
153+
}
154+
```
155+
156+
### **C**
157+
158+
```c
159+
/**
160+
* Note: The returned array must be malloced, assume caller calls free().
161+
*/
162+
int *numberOfPairs(int *nums, int numsSize, int *returnSize) {
163+
int count[101] = {0};
164+
for (int i = 0; i < numsSize; i++) {
165+
count[nums[i]]++;
166+
}
167+
int sum = 0;
168+
for (int i = 0; i < 101; i++) {
169+
sum += count[i] >> 1;
170+
}
171+
int *ans = malloc(sizeof(int) * 2);
172+
ans[0] = sum;
173+
ans[1] = numsSize - sum * 2;
174+
*returnSize = 2;
175+
return ans;
176+
}
126177
```
127178
128179
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *numberOfPairs(int *nums, int numsSize, int *returnSize) {
5+
int count[101] = {0};
6+
for (int i = 0; i < numsSize; i++) {
7+
count[nums[i]]++;
8+
}
9+
int sum = 0;
10+
for (int i = 0; i < 101; i++) {
11+
sum += count[i] >> 1;
12+
}
13+
int *ans = malloc(sizeof(int) * 2);
14+
ans[0] = sum;
15+
ans[1] = numsSize - sum * 2;
16+
*returnSize = 2;
17+
return ans;
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn number_of_pairs(nums: Vec<i32>) -> Vec<i32> {
3+
let n = nums.len();
4+
let mut count = [0; 101];
5+
for &v in nums.iter() {
6+
count[v as usize] += 1;
7+
}
8+
let mut sum = 0;
9+
for v in count.iter() {
10+
sum += v >> 1;
11+
}
12+
vec![sum as i32, (n - sum * 2) as i32]
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function numberOfPairs(nums: number[]): number[] {
2+
const n = nums.length;
3+
const count = new Array(101).fill(0);
4+
for (const num of nums) {
5+
count[num]++;
6+
}
7+
const sum = count.reduce((r, v) => r + (v >> 1), 0);
8+
return [sum, n - sum * 2];
9+
}

0 commit comments

Comments
 (0)