Skip to content

Commit ab40986

Browse files
authored
Create 2225-find-players-with-zero-or-one-losses.js
1 parent e7c6e75 commit ab40986

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[][]} matches
3+
* @return {number[][]}
4+
*/
5+
var findWinners = function(matches) {
6+
const win = {}, lose = {}, total = new Set(), ls = new Set()
7+
for(const [w, l] of matches) {
8+
if(win[w] == null) win[w] = 0
9+
win[w]++
10+
if(lose[l] == null) lose[l] = 0
11+
lose[l]++
12+
total.add(l)
13+
total.add(w)
14+
ls.add(l)
15+
}
16+
17+
const loseKeys = Object.keys(lose)
18+
const a0 = []
19+
for(const e of total) {
20+
if(!ls.has(e)) a0.push(e)
21+
}
22+
const a1 = []
23+
for(const e of loseKeys) {
24+
if(lose[e] === 1) a1.push(e)
25+
}
26+
a0.sort((a, b) => a - b)
27+
a1.sort((a, b) => a - b)
28+
return [a0, a1]
29+
};

0 commit comments

Comments
 (0)