Back to all solutions

#1512 - Number of Good Pairs

Problem Description

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.

Solution

/**
 * @param {number[]} nums
 * @return {number}
 */
var numIdenticalPairs = function(nums) {
  const map = new Array(101).fill(0);
  return nums.reduce((sum, n) => sum += map[n]++, 0);
};