Find the Town Judge

Easy
134.

Find the Town Judge

Find the Town Judge is an easy coding problem on hash table and graph.

In a town of n people (labelled 1..n), the town judge trusts nobody and is trusted by everyone else. trust[i] = [a, b] means person a trusts person b.

Return the judge's label, or -1 if no judge exists.

Example 1

Input
n = 2, trust = [[1,2]]
Output
2

Example 2

Input
n = 3, trust = [[1,3],[2,3],[3,1]]
Output
-1
Explanation
Person 3 trusts someone.

Constraints

  • 1 <= n <= 15
  • 0 <= trust.length <= 60
  • All trust pairs are distinct, a != b.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} n
 * @param {number[][]} trust
 * @return {number}
 */
var findJudge = function(n, trust) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Find the Town Judge — Solution in Python, Java & C++

Find the Town Judge solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Find the Town Judge is an easy coding problem on hash table and graph.

  • Difficulty: Easy
  • Topics: Hash Table, Graph
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

In a town of n people (labelled 1..n), the town judge trusts nobody and is trusted by everyone else. trust[i] = [a, b] means person a trusts person b.

Return the judge's label, or -1 if no judge exists.

Example 1

Input: n = 2, trust = [[1,2]]
Output: 2

Example 2

Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Explanation: Person 3 trusts someone.

Constraints

  • 1 <= n <= 15
  • 0 <= trust.length <= 60
  • All trust pairs are distinct, a != b.

Hints for Find the Town Judge

  1. Hint 1

    Think in-degree minus out-degree.

  2. Hint 2

    The judge scores exactly n-1: trusted by all, trusting none.

How to solve Find the Town Judge

The judge is defined purely by degrees: in-degree n - 1 (everyone trusts them) and out-degree 0 (they trust nobody). Combine the two into a single score — +1 for being trusted, -1 for trusting — and the judge is the unique person scoring exactly n - 1. No graph traversal needed.

Approach

  1. Keep a score per person, all starting at 0.
  2. For each pair [a, b]: decrement score[a] (a trusts someone) and increment score[b] (b is trusted).
  3. Scan people 1..n for one whose score equals n - 1.
  4. Return that label, or -1 if nobody qualifies.

Why it works

Score is in-degree minus out-degree, which is at most n - 1 for anyone: in-degree cannot exceed n - 1 (no self-trust) and out-degree is non-negative. Hitting the maximum therefore forces in-degree to be exactly n - 1 and out-degree exactly 0 — precisely the judge's definition. That also proves uniqueness: two people both trusted by everyone else would have to trust each other, contradicting out-degree 0.

Complexity

  • Time — O(n + m)
  • Space — O(n)

Pitfalls

  • Size the score array n + 1 — people are labelled from 1, not 0.
  • The single combined score is what makes this work; checking in-degree alone wrongly accepts someone who is trusted by all and trusts someone.
  • n = 1 with no trust pairs is a valid town: person 1 scores 0 == n - 1 and is the judge.
  • An empty trust list is allowed, so guard the scan rather than assuming an answer exists.

Reference solution

Python

from typing import List

def findJudge(n: int, trust: List[List[int]]) -> int:
    score = [0] * (n + 1)
    for a, b in trust:
        score[a] -= 1
        score[b] += 1
    for i in range(1, n + 1):
        if score[i] == n - 1:
            return i
    return -1

Java

public static int findJudge(int n, int[][] trust) {
    int[] score = new int[n + 1];
    for (int[] t : trust) {
        score[t[0]]--;
        score[t[1]]++;
    }
    for (int i = 1; i <= n; i++) {
        if (score[i] == n - 1) return i;
    }
    return -1;
}

C++

int findJudge(int n, vector<vector<int>>& trust) {
    vector<int> score(n + 1, 0);
    for (const auto& t : trust) {
        score[t[0]]--;
        score[t[1]]++;
    }
    for (int i = 1; i <= n; i++) {
        if (score[i] == n - 1) return i;
    }
    return -1;
}

JavaScript

var findJudge = function(n, trust) {
    const score = new Array(n + 1).fill(0);
    for (const t of trust) {
        score[t[0]]--;
        score[t[1]]++;
    }
    for (let i = 1; i <= n; i++) {
        if (score[i] === n - 1) return i;
    }
    return -1;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 439 hash table problems · the whole catalogue

Learn the technique: Hashing: Hash Maps and Hash Sets · Graph Data Structure