Problem statement

https://binarysearch.com/problems/Mutual-Followers/

Solution

Just use set and check for pair (x, y) if we have (y, x).

Complexity

It is O(m + n log n) for time and O(n) for space.

Code

class Solution:
    def solve(self, R):
        s = set([(x, y) for x, y in R])
        ans = set()
        for x, y in s:
            if (y, x) in s:
                ans |= set([x, y])
        return sorted(ans)