Skip to main content

1512. Number of Good Pairs

https://leetcode.com/problems/number-of-good-pairs

Python

from math import factorial
from collections import defaultdict


class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
mapper = defaultdict(int)

for i, num in enumerate(nums):
mapper[num] += 1

ans = 0
for count in mapper.values():
if count >= 2:
# C = n! / m!(n-m!)
ans += (
factorial(count)/
(2*factorial(count-2))
)

return int(ans)