Skip to content

Commit 2be5e0c

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 9.8 MB (22.81%)
1 parent 17a1cff commit 2be5e0c

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<p>Given an array of integers <code>nums</code>, return <em>the number of <strong>good pairs</strong></em>.</p>
2+
3+
<p>A pair <code>(i, j)</code> is called <em>good</em> if <code>nums[i] == nums[j]</code> and <code>i</code> &lt; <code>j</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> nums = [1,2,3,1,1,3]
10+
<strong>Output:</strong> 4
11+
<strong>Explanation:</strong> There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [1,1,1,1]
18+
<strong>Output:</strong> 6
19+
<strong>Explanation:</strong> Each pair in the array are <em>good</em>.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums = [1,2,3]
26+
<strong>Output:</strong> 0
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
34+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
35+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int numIdenticalPairs(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<int> freq(101, 0);
6+
int ans = 0;
7+
for (int num : nums) {
8+
ans += freq[num];
9+
freq[num]++;
10+
}
11+
return ans;
12+
}
13+
};

0 commit comments

Comments
 (0)