Common songs across playlists

Write codeMedium15 min

Your task

A music service wants the songs that every one of its editorial playlists agrees on. Given a list of playlists, each a list of integer song ids (a playlist may repeat an id), return the ids that appear in every playlist, sorted ascending. If the outer list is empty, return an empty list; a single playlist returns its distinct ids sorted. For example, [[1, 2, 3], [2, 3, 4], [3, 2, 9]] returns [2, 3]. Intersecting lists pairwise with repeated scans is the slow path, count memberships instead.

Examples

common_to_all(playlists: list[list[int]]) -> list[int]
Example 1
Inputcommon_to_all([[1, 2, 3], [2, 3, 4], [3, 2, 9]])
Output[2, 3]