Last active
November 6, 2020 04:06
-
-
Save GAierken/e07a75ba6ce2c6195a801f433c30a051 to your computer and use it in GitHub Desktop.
findTheTownJudge.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const findJudge = (N, trust) => { | |
// keep track of how many likes the element gives | |
let likesCountList = {} | |
//keep track of how many likes the element receives | |
let beingLikedCountList = {} | |
//hash the key from 1 to N | |
for(let i = 1; i <= N; i++){ | |
likesCountList[i] = 0 | |
beingLikedCountList[i] = 0 | |
} | |
//loop through trust to hash value to hashes | |
for(let ele of trust){ | |
likesCountList[ele[0]]++ | |
beingLikedCountList[ele[1]]++ | |
} | |
//variable to store potential judge | |
let judge = 0 | |
//if the element doesn't give out any likes | |
//it is the potantial judge | |
for(key in likesCountList){ | |
if(likesCountList[key] === 0) judge = key | |
} | |
//if the potential judge receives likes from everybody other than itself | |
//it means it is the judge | |
//otherwise judge doesn't exist | |
if(beingLikedCountList[judge] === N - 1) return judge | |
else return -1 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment