Skip to content

Commit f4903fc

Browse files
committed
https://leetcode.cn/problems/number-of-good-pairs
1 parent 1475f55 commit f4903fc

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/number-of-good-pairs
14+
1315
https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string
1416

1517
https://leetcode.cn/problems/minimum-height-tree-lcci/

number-of-good-pairs/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function numIdenticalPairs(nums: number[]): number {
2+
const cnt = new Map<number, number>();
3+
for (const num of nums) {
4+
cnt.set(num, (cnt.get(num) ?? 0) + 1);
5+
}
6+
const ans = Array.from(cnt.values()).reduce(
7+
(a, v) => a + Math.floor((v * (v - 1)) / 2),
8+
0
9+
);
10+
return ans;
11+
}

0 commit comments

Comments
 (0)