The title

Give you an integer array nums.

A set of numbers (I,j) is considered a good pair if nums[I] == nums[j] and I < j.

Returns the number of good pairs.

 

Example 1: input: nums = [1,2,3,1,1,3] output: 4 description: there are four pairs of good numbers, which are (0,3), (0,4), (3,4), (2,5) with subscripts starting from 0 example 2: input: nums = [1,1,1] output: Example 3: input: nums = [1,2,3] output: 0Copy the code

Tip:

1 <= nums.length <= 100 1 <= nums[i] <= 100

Their thinking

Class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: nums = 0 # for I in range(len(nums)): # for j in range(i+1,len(nums)): # if nums[i] == nums[j]: From collections import Counter numsDic = Counter(nums) res = 0 for val in numsDic.values(): res += (val*(val-1))//2 return res if __name__ == '__main__': Nums = [1,2,3,1, 3] ret = Solution(). NumIdenticalPairs (nums) print(ret)Copy the code