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

Source: leetcode | Difficulty: EASY
Tags: Data structures

Mathematics

Starter code: class Solution:
    def numIdenticalPairs(self, nums: List[int]) -> int:
        

Solutions:
class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		my_count = 0
		my_dict = {}
		for n in nums:
			my_count += my_dict.get(n, 0)
			my_dict[n] = my_dict.get(n, 0) + 1
		return my_count


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		hashMap = {}
		res = 0
		for number in nums:
			if number in hashMap:
				res += hashMap[number]
				hashMap[number] += 1
			else:
				hashMap[number] = 1
		return res


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		count = 0
		for i in range(len(nums)):
			for j in range(i + 1, len(nums)):
				if nums[i] == nums[j]:
					count += 1
		return count


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		for i in nums:
			repeat = {}
			n = 0
			for i in nums:
				if i in repeat:
					n += repeat[i]
					repeat[i] += 1
				else:
					repeat[i] = 1
		return n


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		num_good = 0
		for i in range(len(nums)):
			first = nums[i]
			for j in nums[i + 1:]:
				print((first, j))
				if first == j:
					num_good += 1
		return num_good


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		return sum((c * (c - 1) // 2 for c in Counter(nums).values()))


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		good = 0
		num_dict = {}
		for num in nums:
			if num in num_dict:
				good += num_dict[num]
				num_dict[num] += 1
			else:
				num_dict[num] = 1
		return good


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		uniques = set(nums)
		count = 0
		for unique in uniques:
			count += nums.count(unique) * (nums.count(unique) - 1) / 2
		return int(count)


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		(res, map) = (0, {})
		for num in nums:
			if num not in map:
				map[num] = 1
			else:
				res += map[num]
				map[num] += 1
		return res


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		tracker = {}
		for num in nums:
			if num not in tracker:
				tracker[num] = 0
			else:
				tracker[num] += 1
		sum = 0
		for num in tracker:
			sum += tracker[num] * (tracker[num] + 1) // 2
		return sum


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		pairCount = 0
		i = 0
		while i < len(nums) - 1:
			j = i + 1
			while j < len(nums):
				if nums[i] == nums[j]:
					pairCount += 1
				j += 1
			i += 1
		return pairCount


class Solution:

	def numIdenticalPairs(self, nums: List[int]) -> int:
		counter = collections.Counter(nums)
		answer = 0
		for (k, v) in counter.items():
			answer += v * (v - 1) // 2
		return answer