-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Given an array of integers nums. | ||
|
||
A pair (i,j) is called good if nums[i] == nums[j] and i < j. | ||
|
||
Return the number of good pairs. | ||
|
||
|
||
|
||
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 | ||
|
||
|
||
Constraints: | ||
|
||
1 <= nums.length <= 100 | ||
1 <= nums[i] <= 100 | ||
*/ | ||
|
||
class Solution: | ||
def numIdenticalPairs(self, nums: List[int]) -> int: #{ | ||
num_dict = {} | ||
for i in nums: #{ | ||
if str(i) in num_dict.keys(): | ||
num_dict[str(i)] += 1 | ||
else: num_dict[str(i)] = 1 | ||
#} | ||
res = 0 | ||
for value in num_dict.values(): #{ | ||
for i in range(1, value): #{ | ||
res+=i | ||
#} | ||
#} | ||
return res | ||
#} | ||
|