[LeetCode] Number of Good Pairs

1512. Number of Good Pairs

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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {
unordered_map<int,int> freq;
int res = 0;
for(auto& n : nums) {
res += freq[n];
freq[n] += 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/10/03/PS/LeetCode/number-of-good-pairs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.