Editor

Common to Every List

#78 · Lists · Intermediate · 70 XP · about 14 min

Write common_items(lists) that takes a list of one or more lists and returns a list of the items that appear in every one of them.

Repeats count: an item appears in the answer as many times as its smallest count across the lists, so an item found at least twice in every list appears twice. Keep the order of the first list, using its earliest copies of each item.

Examples

common_items([[1, 2, 3], [3, 1, 4], [5, 1, 3]])        # returns [1, 3]
common_items([[2, 1, 2, 3], [2, 2, 1], [1, 2, 4, 2]])  # returns [2, 1, 2]
common_items([['x', 'y']])                             # returns ['x', 'y']
common_items([[1, 2], []])                             # returns []

In the second example 2 appears twice in every list and 1 once, so the answer holds two 2s and one 1, in the order of the first list.

Constraints

  • 1 <= len(lists) <= 100
  • Each inner list has 0 to 10,000 items (numbers or strings)

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.