Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions C++/1512. good pairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <bits/stdc++.h>

using namespace std;

int numIdenticalPairs(vector<int>& nums) {
unordered_map<int, int> count;

// Count the occurrences of each number
for (int num : nums) {
count[num]++;
}

int goodPairs = 0;

// Calculate the number of good pairs for each number
for (const auto& pair : count) {
int occurrences = pair.second;
if (occurrences >= 2) {
goodPairs += (occurrences * (occurrences - 1)) / 2;
}
}

return goodPairs;
}

int main() {
vector<int> nums = {1, 2, 3, 1, 1, 3, 4};
int result = numIdenticalPairs(nums);
cout << "Number of Good Pairs: " << result << endl;

return 0;
}