""" 1512.1 - Solution 1 - Brute Force Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def numIdenticalPairs(self, nums: List[int]) -> int:
"""Number of Good Pairs Function"""
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
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().numIdenticalPairs([1, 2, 3, 1, 1, 3])) # 4
print(Solution().numIdenticalPairs([1, 1, 1, 1])) # 6
print(Solution().numIdenticalPairs([1, 2, 3])) # 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1512.2 - Solution 2 - Hash Map (Counting) """
#####################################################################################
# Imports
#####################################################################################
from typing import List
from collections import Counter
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def numIdenticalPairs(self, nums: List[int]) -> int:
"""Number of Good Pairs Function"""
ans = 0
cnt = Counter()
for x in nums:
ans += cnt[x]
cnt[x] += 1
return ans
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().numIdenticalPairs([1, 2, 3, 1, 1, 3])) # 4
print(Solution().numIdenticalPairs([1, 1, 1, 1])) # 6
print(Solution().numIdenticalPairs([1, 2, 3])) # 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()