Skip to main content
Back to problems
#1128
Easy Algorithms

Number of equivalent domino pairs

Array Hash Table Counting
60.6% acceptance
Feb 25, 2026
1117
376
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].

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
use std::collections::HashMap;

impl Solution {
  pub fn num_equiv_domino_pairs(dominoes: Vec<Vec<i32>>) -> i32 {
    let mut count: HashMap<(i32, i32), i32> = HashMap::new();
    let mut result = 0;
    for d in &dominoes {
      let key = (d[0].min(d[1]), d[0].max(d[1]));
      let c = count.entry(key).or_insert(0);
      result += *c;
      *c += 1;
    }
    result
  }
}