Number of Equivalent Domino Pairs - Problem

Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either:

  • (a == c and b == d), or
  • (a == d and b == c)

That is, one domino can be rotated to be equal to another domino.

Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].

Input & Output

Example 1 — Basic Case
$ Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
Output: 1
💡 Note: [1,2] and [2,1] are equivalent (one can be rotated to match the other), so there's 1 equivalent pair
Example 2 — Multiple Pairs
$ Input: dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
Output: 3
💡 Note: Three [1,2] dominoes form 3 pairs: (0,1), (0,3), (1,3). Other dominoes don't have matches.
Example 3 — No Equivalent Pairs
$ Input: dominoes = [[1,1],[2,2],[1,3]]
Output: 0
💡 Note: No two dominoes are equivalent, so there are 0 pairs

Constraints

  • 1 ≤ dominoes.length ≤ 4 × 104
  • dominoes[i].length == 2
  • 1 ≤ dominoes[i][j] ≤ 9

Visualization

Tap to expand
Number of Equivalent Domino PairsInput Dominoes:[1,2][2,1][1,1][1,1]Find Equivalent Pairs:[1,2] ≡ [2,1][1,1] ≡ [1,1]Pair (0,1)Pair (2,3)Result: 2 equivalent pairs
Understanding the Visualization
1
Input
Array of dominoes where each domino has two values
2
Process
Find dominoes that are equivalent (same values, possibly rotated)
3
Output
Count of equivalent pairs where i < j
Key Takeaway
🎯 Key Insight: Normalize dominoes by sorting values to handle rotation, then count frequencies and calculate pairs using n*(n-1)/2 formula
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen