Find center of star graph

Implementation

class Solution:

    def findCenter(self, edges: List[List[int]]) -> int:

        # Brute force approach

        nodes, count = defaultdict(int), 0

        for i in edges:

            count += 1

            for j in i:

                nodes[j] += 1

        for k in nodes:

            if nodes[k] >= len(nodes) - 1:

                return k
class Solution:

    def findCenter(self, edges: List[List[int]]) -> int:

        # Optimal solution

        firstEdge, secondEdge = edges[0], edges[1]

        if (firstEdge[0] == secondEdge[0] or firstEdge[0] == secondEdge [1]):

            return firstEdge[0]

        else:

            return firstEdge[1]