Skip to content

Instantly share code, notes, and snippets.

@GAierken
Last active November 6, 2020 04:06
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 GAierken/e07a75ba6ce2c6195a801f433c30a051 to your computer and use it in GitHub Desktop.
Save GAierken/e07a75ba6ce2c6195a801f433c30a051 to your computer and use it in GitHub Desktop.
findTheTownJudge.js
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