Skip to content

Commit 33030a9

Browse files
committed
✨ [1512] Number of Good Pairs
1 parent 8c06828 commit 33030a9

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

1512/my_solution.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const numIdenticalPairs = (nums) => { // from chat gpt
6+
let freq = {};
7+
let count = 0;
8+
9+
// Count the frequency of each number
10+
for (let num of nums) {
11+
freq[num] = (freq[num] || 0) + 1;
12+
}
13+
14+
console.log(freq)
15+
// Calculate the number of good pairs for each number
16+
for (let key in freq) {
17+
let frequency = freq[key];
18+
console.log(`${key} -> ${(frequency - 1) * frequency / 2}`)
19+
count += ((frequency - 1) * frequency) / 2;
20+
}
21+
22+
return count;
23+
}
24+
25+
numIdenticalPairs([1, 2, 3, 1, 1, 3])
26+
27+
// const numIdenticalPairs = (nums) => {
28+
// let count = 0;
29+
//
30+
// for (let i = 0; i < nums.length; i++) {
31+
// for (let j = 0; j < nums.length; j++) {
32+
// if (i < j && nums[i] === nums[j]) count++;
33+
// }
34+
// }
35+
//
36+
// return count;
37+
// }

1512/solution.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const numIdenticalPairs = (nums) => { // from chat gpt
6+
let freq = {};
7+
let count = 0;
8+
9+
// Count the frequency of each number
10+
for (let num of nums) {
11+
freq[num] = (freq[num] || 0) + 1;
12+
}
13+
14+
// Calculate the number of good pairs for each number
15+
for (let key in freq) {
16+
let frequency = freq[key];
17+
count += (frequency - 1) * frequency / 2;
18+
}
19+
20+
return count;
21+
}
22+
23+
numIdenticalPairs([1, 2, 3, 1, 1, 3])

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [647. Palindromic Substrings](./647/)
5959
- [771. Jewels and Stones](./771/)
6060
- [997. Find the Town Judge](./997/)
61+
- [1512. Number of Good Pairs](./1512/)
6162
- [1791. Find Center of Star Graph](./1791/)
6263
- [1971. Find if Path Exists in Graph](./1971/)
6364

@@ -98,8 +99,8 @@ Batch create:
9899
<!--
99100
NOTE: JS IS HERE
100101
-->
101-
```**ssh**
102-
chapter=56 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
102+
```ssh
103+
chapter=1512 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
103104
```
104105
> then you can use `x` for quick debug.
105106

0 commit comments

Comments
 (0)