Skip to main content
Back to problems
Leetcode
Easy
Arrays
Hash Maps
Combinatorics
Number Of Good Pairs

Count the number of index pairs whose values are equal.

Acceptance 0%
Problem Statement

You are given an integer array nums. A pair of indices (i, j) is called a good pair if:

  • 0 <= i < j < nums.length
  • nums[i] == nums[j]

Return the total number of good pairs in the array.

The task is to count all valid index pairs, not just distinct values.

Input Format

  • An integer array nums.
  • Each element is an integer.

In an interview setting, the array is provided directly as input; on platforms it is typically given in the problem's standard format.

Output Format

  • Return a single integer: the number of good pairs (i, j) such that i < j and nums[i] == nums[j].

Constraints

  • 1 <= nums.length
  • Values may repeat multiple times.
  • Count each valid index pair exactly once.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,2,3,1,1,3]

Output

4

Explanation

The good pairs are (0,3), (0,4), (3,4), and (2,5).

Example 2

Input

nums = [1,1,1,1]

Output

6

Explanation

Every pair of indices is good. There are 4 choose 2 = 6 such pairs.

Show 1 more example

Example 3

Input

nums = [1,2,3]

Output

0

Explanation

No two different indices contain the same value.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.