332. Number of Good Pairs
Easy
Frequency Pair Counting
Hashing
Problem
Given an array of integers nums, return the number of good pairs. A pair of indices (i, j) is good when nums[i] equals nums[j] and i is less than j.
Examples
Example 1
Input: [1,2,3,1,1,3]
Output: 4
Example 2
Input: [1,1,1,1]
Output: 6
Example 3
Input: [1,2,3]
Output: 0
Constraints
� 1 <= nums.length <= 100
� 1 <= nums[i] <= 100
Hints
?? When processing a value, every previous occurrence creates one new pair.
?? Alternatively, a frequency f contributes f * (f - 1) / 2 pairs.
Expected Complexity
Time: O(n)
Space: O(n)
Follow-up
Can you count pairs online in one pass without a second traversal?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,2,3,1,1,3]Expected
4