> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Good Pairs Python Solution

> Tested Python solution for LeetCode 1512 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1512, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/number-of-good-pairs/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1512   # by problem number
lcpy gen -s number_of_good_pairs   # by problem name
```

## Problem

Given an array of integers `nums`, return *the number of **good pairs***.

A pair `(i, j)` is called *good* if `nums[i] == nums[j]` and `i` \< `j`.

### Examples

```
Input: nums = [1,2,3,1,1,3]
Output: 4
```

**Explanation:** There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.

```
Input: nums = [1,1,1,1]
Output: 6
```

**Explanation:** Each pair in the array are *good*.

```
Input: nums = [1,2,3]
Output: 0
```

### Constraints

* 1 \<= nums.length \<= 100
* 1 \<= nums\[i] \<= 100

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_good_pairs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_good_pairs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def num_identical_pairs(self, nums: list[int]) -> int:
        counts: dict[int, int] = {}
        result = 0
        for num in nums:
            seen = counts.get(num, 0)
            result += seen
            counts[num] = seen + 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.