Skip to Main Content

Number of Good Pairs

Problem URL:Number of Good Pairs

My Solution

JavaScript

function numIdenticalPairs(nums: number[]): number {
  let pairCount = 0;
  const hashMap: Record<string, number[]> = {};

  for (let i = 0; i < nums.length; i++) {
    if (nums[i] in hashMap) {
      hashMap[nums[i]].push(i);
      pairCount += hashMap[nums[i]].length - 1;
    } else {
      hashMap[nums[i]] = [i];
    }
  }

  return pairCount;
}

Let's Connect

Twitter GitHub LinkedIn