#997. Find the Town Judge

ยท

1 min read

https://leetcode.com/problems/find-the-town-judge/description/?envType=daily-question&envId=2024-02-22

/**
 * @param {number} n
 * @param {number[][]} trust
 * @return {number}
 */
var findJudge = function (n, trust) {

    const trusted = new Array(n + 1).fill(0)

    for (const [a, b] of trust) {
        trusted[a]--
        trusted[b]++
    }

    for (let i = 1; i <= n; i++) {
        if (trusted[i] === n - 1) {
            return i
        }
    }

    return -1
};
ย