45. Count Good Pairs

EasyArrayArray

Given an integer array nums, return the number of good pairs. A pair (i, j) is good if nums[i] == nums[j] and i < j.

Input: An integer array nums of length n.

Output: Integer — count of good pairs.

Examples

Example 1
Input: [1,2,3,1,1,3]
Output: 4
Explanation: (0,3),(0,4),(3,4) for value 1 = C(3,2)=3 pairs. (2,5) for value 3 = 1 pair. Total = 4.
Example 2
Input: [1,1,1,1]
Output: 6
Explanation: All 4 elements equal 1. C(4,2) = 6 pairs.
Example 3
Input: [1,2,3]
Output: 0
Explanation: All elements distinct. No good pairs.

Constraints

Asked by

AccentureBloombergGoogleMicrosoftAmazonMeta
Solve this problem in the editor →