FunctionRacerProblemsRace

Friends in Common

BeginnerGraphsPython, JavaScript, TypeScript, Java, C++

Each entry of friendships is a pair [x, y]: persons x and y are friends. Friendship goes both ways, and a pair may be listed more than once.

Return the persons who are friends with both a and b, in increasing order and each only once.

Example 1

friendships [[0,2],[2,1],[0,3]]
a 0
b 1
returns [2]

Person 2 is a friend of both 0 and 1. Person 3 is a friend of 0 only.

Example 2

friendships [[0,1]]
a 0
b 1
returns []

Persons 0 and 1 are friends with each other, but they share no third friend.

Example 3

friendships []
a 0
b 1
returns []

Nobody has any friends.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems