Skip to content

Commit 25324fc

Browse files
authored
Create 1512. good pairs.cpp
Solved issue codedecks-in#439
1 parent 80c004e commit 25324fc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

C++/1512. good pairs.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int numIdenticalPairs(vector<int>& nums) {
5+
unordered_map<int, int> count;
6+
7+
for (int num : nums) {
8+
count[num]++;
9+
}
10+
11+
int goodPairs = 0;
12+
for (const auto& pair : count) {
13+
int occurrences = pair.second;
14+
if (occurrences >= 2) {
15+
goodPairs += (occurrences * (occurrences - 1)) / 2;
16+
}
17+
}
18+
19+
return goodPairs;
20+
}
21+
22+
int main() {
23+
vector<int> nums = {1, 2, 3, 1, 1, 3, 4};
24+
int result = numIdenticalPairs(nums);
25+
cout << "Number of Good Pairs: " << result << endl;
26+
27+
return 0;
28+
}

0 commit comments

Comments
 (0)