Skip to content

Commit

Permalink
Create 1512. Number of Good Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
DJLee68 authored Feb 21, 2021
1 parent 6d2cbe3 commit 0b99c1f
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions algorithms/Python/Easy/1512. Number of Good Pairs
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
#}

0 comments on commit 0b99c1f

Please sign in to comment.