Show problems as
Classic problem #1512EasyArrayHash TableMathCounting

Number of Good Pairs

Real-world version: Matched Spoke Pairs
Tracked in this browser only
Read Number of Good Pairs on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A wheelbuilder keeps loose spokes in a rack. lengths[i] is the length in millimetres of the spoke in slot i.

Two spokes make a matched pair when they sit in different slots and read the same length. A pair is named by its two slots, so the pair from slots 2 and 5 is the same pair as the one from slots 5 and 2, and a spoke never pairs with itself.

Return the number of matched pairs in the rack. Pairs are counted over all slots at once, so a length held in four slots contributes every pair those four slots make.

Examples

Example 1

Input
lengths = [64, 17, 64, 64, 29]
Output
3

Length 64 sits in slots 0, 2 and 3, which make the pairs (0,2), (0,3) and (2,3). The spokes at 17 and 29 have no partner.

Example 2

Input
lengths = [46, 82, 91]
Output
0

The three spokes all read different lengths, so the rack holds no matched pair.

Example 3

Input
lengths = [7, 7, 7, 7, 7, 7]
Output
15

All six slots read 7, and six slots make fifteen distinct pairs.

Constraints

  • 1 <= lengths.length <= 100
  • 1 <= lengths[i] <= 100

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def matched_spoke_pairs(lengths: list[int]) -> int:
Java
public int matchedSpokePairs(int[] lengths)