Skip to content

Instantly share code, notes, and snippets.

@perjerz
Created April 25, 2022 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perjerz/048a3257f0b9354e2f00d8046997e18a to your computer and use it in GitHub Desktop.
Save perjerz/048a3257f0b9354e2f00d8046997e18a to your computer and use it in GitHub Desktop.
Algorithm: Winning Card Problem - JavaScript
// https://leetcode.com/discuss/interview-question/1768919/winning-card
function solution(cards) {
// write your solution here
let winner = -1;
let players = [];
for (let card of cards) {
let max = -1;
const dict = {};
for (let c of card) {
dict[c] = dict[c] ? dict[c]+1 : 1;
}
const max1 = Object.keys(dict).filter(k => dict[k] === 1).map(k => Number(k));
if (max1.length > 0) {
max = Math.max(...max1);
}
players.push(max);
}
if (players.length === 0) {
return -1;
}
return Math.max(...players);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment