Given an array of integers
nums
, return the number of good pairs.
A pair(i, j)
is called good ifnums[i] == nums[j]
andi
<j
.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
Example 1:
Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Example 2:
Input: nums = [1,1,1,1]
Output: 6
Explanation: Each pair in the array are good.
Example 3:
Input: nums = [1,2,3]
Output: 0
class Solution {
public:
int numIdenticalPairs(vector<int>& nums) {
map<int, int> counter;
int ans = 0;
for(int i = 0; i < nums.size(); i++)
{
ans += counter[nums[i]];
counter[nums[i]]++;
}
return ans;
}
};
LeetCode
C++
1. Two Sum
Nov 15, 2023You are given an array of positive integers arr. Perform some operations (possibly none) on arr so that it satisfies these conditions:
Nov 15, 2023Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.
Nov 9, 2023There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
Nov 9, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up