Zepruv › Coding problems

Number of Good Pairs

Easy · Array, Hash Table, Math, Counting

Given an array of integers nums, return the number of good pairs.
A pair (i, j) is called good if nums[i] == nums[j] and i < j.

Example 1

Input: nums = [1,2,3,1,1,3]
Output: 4

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

Each pair in the array are good.

Example 3

Input: nums = [1,2,3]
Output: 0

Constraints

Starter code (python)

class Solution(object):
    def numIdenticalPairs(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        

Related problems

Previous: Minimum Number of Increments on Subarrays to Form a Target Array · Next: Number of Substrings With Only 1s · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.