Count the number of index pairs whose values are equal.
You are given an integer array nums. A pair of indices (i, j) is called a good pair if:
0 <= i < j < nums.lengthnums[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 thati < jandnums[i] == nums[j].
Constraints
1 <= nums.length- Values may repeat multiple times.
- Count each valid index pair exactly once.
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.