Skip to the code
cf.
+10 XP

#651 Matching Swatch Pairs

easy

A palette editor flags duplicate swatches. colors lists the swatch colors, possibly with repeats.

Write duplicatePairs(colors) that returns how many pairs of positions i < j have colors[i] === colors[j].

Example 1

Input: colors = ["red","blue","red","red"]
Output: 3

The three red swatches make C(3, 2) = 3 pairs.

Example 2

Input: colors = ["a","b"]
Output: 0

No color appears twice.

Constraints

  • 0 <= colors.length <= 10^5
  • Colors are short strings.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).