Skip to content

Commit c19a4e1

Browse files
authored
Create number-of-good-pairs.cpp
1 parent 85e3855 commit c19a4e1

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/number-of-good-pairs.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int numIdenticalPairs(vector<int>& nums) {
7+
int result = 0;
8+
for (const auto& [n, c] : counter(nums)) {
9+
result += c * (c - 1) / 2;
10+
}
11+
return result;
12+
}
13+
14+
private:
15+
unordered_map<int, int> counter(const vector<int>& arr) {
16+
unordered_map<int, int> result;
17+
for (const auto& i : arr) {
18+
++result[i];
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)