> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Number of Good Pairs

Leetcode Problem 1512: Number of Good Pairs

Problem Summary

Given an array of integers, count the number of good pairs — pairs of indices (i, j) where i < j and nums[i] == nums[j].

Constraints:

  • The array has between 1 and 100 elements.
  • Each element is a positive integer between 1 and 100.

Techniques

  • Array
  • Hash Table
  • Math
  • Counting

Solution

function numIdenticalPairs(nums: number[]): number {
    const numbersCount = new Map<number, number>()
    let countGoodPairs = 0

    for (let i = 0; i < nums.length; i++) {
        if (numbersCount.has(nums[i])) {
            countGoodPairs = countGoodPairs + numbersCount.get(nums[i])!
        }

        numbersCount.set(nums[i], (numbersCount.get(nums[i]) || 0) + 1);
    }

    return countGoodPairs
};

console.log(numIdenticalPairs([1,2,3,1,1,3]))