-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardGame.js
More file actions
35 lines (33 loc) · 1.02 KB
/
cardGame.js
File metadata and controls
35 lines (33 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// In a card game, each player will be given a set of random cards. Players will throw on the table their one
// winning card, the player with the highest card wins. A winning card is the card that exists once in
// the set of cards and the highest one.
// Given an array of sets of integers: cards, return the card of the winning player. Return -1 if no such card is found.
// SAMPLE 1: [[5,7,3,9,4,9,8,3,2], [1,2,2,4,4,1], [1,2,3]] => 8
// SAMPLE 2: [[5,5], [2,2]] => -1
function cardGame(arr) {
const lenOfArr = arr.length;
const uniqueArr = [];
for (let i = 0; i < lenOfArr; i++) {
const element = arr[i];
const len = element.length;
for (let j = 0; j < len; j++) {
if (element.lastIndexOf(element[j]) === element.indexOf(element[j])) {
uniqueArr.push(element[j]);
}
}
}
return uniqueArr.length > 0 ? Math.max(...uniqueArr) : -1;
}
console.log(
cardGame([
[5, 7, 3, 9, 4, 9, 8, 3, 2],
[1, 2, 2, 4, 4, 1],
[1, 2, 3],
])
);
console.log(
cardGame([
[5, 5],
[2, 2],
])
);