Skip to content

Commit eac6049

Browse files
authored
Create 1128-number-of-equivalent-domino-pairsnumber-of-equivalent-domino-pairs.js
1 parent 118d8ac commit eac6049

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {number[][]} dominoes
3+
* @return {number}
4+
*/
5+
const numEquivDominoPairs = function(dominoes) {
6+
const hash = {}
7+
for (let dom of dominoes) {
8+
const [a, b] = dom
9+
const key = `${a},${b}`, alterKey = `${b},${a}`
10+
if (hash[key] == null && hash[alterKey] == null) {
11+
hash[key] = 1
12+
} else {
13+
if(hash[key] != null) hash[key] += 1
14+
else hash[alterKey] += 1
15+
}
16+
}
17+
18+
let res = 0
19+
20+
Object.keys(hash).forEach(k => {
21+
if(hash[k] > 1) res += sum(hash[k])
22+
})
23+
24+
return res
25+
};
26+
27+
function sum(n) {
28+
let res = 0
29+
while(n > 1) {
30+
res += n - 1
31+
n--
32+
}
33+
return res
34+
}

0 commit comments

Comments
 (0)