Skip to the code
cf.
+10 XP

#406 Count Mutual Follows

easy

A profile page shows a "mutuals" badge when two users follow each other. You get the follow list follows, where [a, b] means user a follows user b.

Write countMutuals(follows) that returns how many unordered pairs of users follow each other. The pair {a, b} counts once, no matter the order of the two follows.

No follow appears twice and nobody follows themselves.

Example 1

Input: follows = [[0,1],[1,0],[1,2],[2,3],[3,2]]
Output: 2

Users 0 and 1 follow each other, and so do 2 and 3. The follow from 1 to 2 is one way.

Example 2

Input: follows = [[0,1],[1,2]]
Output: 0

No follow goes back the other way.

Constraints

  • 0 <= follows.length <= 100000
  • 0 <= a, b <= 10^9 and a !== b
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).