In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:


	The town judge trusts nobody.
	Everybody (except for the town judge) trusts the town judge.
	There is exactly one person that satisfies properties 1 and 2.


You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi.

Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.

&nbsp;
Example 1:

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


Example 2:

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


Example 3:

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


&nbsp;
Constraints:


	1 &lt;= n &lt;= 1000
	0 &lt;= trust.length &lt;= 104
	trust[i].length == 2
	All the pairs of trust are unique.
	ai != bi
	1 &lt;= ai, bi &lt;= n


class Solution:
    def findJudge(self, n: int, trust: List[List[int]]) -> int:
        possible = set([i for i in range(1,n+1)])
        in_edges = [0] * (n+1)
        
        for first, second in trust:
            possible.discard(first)
            in_edges[second] += 1
            
        if len(possible) != 1:
            return -1
        
        i = possible.pop()
        if in_edges[i] == n-1:
            return i
        return -1
class Solution {
    public int findJudge(int n, int[][] trust) {
        int count=0;
        int x[]=new int[n+1];
        int y[]=new int[n+1];
         Arrays.fill(x, 0);
         Arrays.fill(y, 0);
        for(int i=0;i<trust.length;i++)
        {
            x[trust[i][0]]=1;
        }
        for(int i=1;i<=n;i++)
        {
            if(x[i]!=0)
            count++;
            if(x[i]==0)
            y[i]=1;
        }
        if(count==n)
        return -1;
       for(int i=1;i<=n;i++)
       System.out.println(y[i]);
       int jud=0;
         for(int i=1;i<=n;i++)
        {
            if(y[i]==1)
            jud=i;
        }
        int c=0;
        for(int i=0;i<trust.length;i++)
        {
            if(trust[i][1]==jud)
            c++;
        }
        if(c==n-1)
        return jud;
        return -1;
       
    }
}
class Solution {
public:
    int findJudge(int n, vector<vector<int>>& trust) {
        vector<pair<int,int>> v(n+1,{0,0});
        
        for(auto i : trust){
            v[i[0]].first++;
            v[i[1]].second++;
        }
        
        int ans = -1;
        

        for(int i=1;i<=n;i++){
            auto p = v[i];
            if(p.first == 0 && p.second == n-1){
                ans = i;
                break;
            }
        }
        
        return ans;
    }
};
var findJudge = function(n, trust) {
    const length = trust.length;
    let possibleJudge = [], judgeMap = new Map(), value, judge = -1;
    for(let i = 0; i < length; i++) {
        if(judgeMap.has(trust[i][0])){
            value = judgeMap.get(trust[i][0]);
            value.push(trust[i][1]);
            judgeMap.set(trust[i][0], value);
        }
        else {
            judgeMap.set(trust[i][0], [trust[i][1]]);
        }
    }
    value = [];
    for(let i = 1; i <= n; i++) {
        if(!judgeMap.has(i)) {
            possibleJudge.push(i);
        }
        else {
            value.push([i,judgeMap.get(i)]);
        }
    }
    if(!value.length || value.length !== n-1) {
        if(possibleJudge.length === 1) return possibleJudge[0];
        return judge;
    }
    for(let i = 0; i < possibleJudge.length; i++) {
        judge = possibleJudge[i];
        for(let j = 0; j < value.length; j++) {
            if(value[j][1].indexOf(judge) < 0) {
                judge = -1;
                break;
            }
        }
        if(judge !== -1) break;
    }
    return judge;
};
Find the Town Judge