Python 3 https://leetcode.com/problems/maximal-network-rank

class Solution:
    def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
        d = collections.defaultdict(set)
        for a, b in roads: 
            d[a].add(b)
            d[b].add(a)
        res = 0
        for i in d: 
            for j in d: 
                if i != j: 
                    res = max(res, len(d[i]) + len(d[j]) - (i in d[j]))
 
        return res