easy +5 pts

Set intersection size

Compute the number of common elements between two lists.

Write a function `def intersection_size(list_a, list_b):` that returns the number of distinct elements that appear in both `list_a` and `list_b`. - The lists can contain integers, strings, or any hashable values. - Duplicates within a single list should be counted only once. - The order of elements does not matter. For example, given `[1, 2, 2, 3]` and `[2, 3, 4]`, the distinct common elements are `2` and `3`, so the answer is `2`.

Constraints

- `0 <= len(list_a), len(list_b) <= 10^5` - Elements are hashable. - Time: O(n + m) on average. - Space: O(min(n, m)) or O(n) depending on implementation.

Example

>>> intersection_size([1, 2, 2, 3], [2, 3, 4])
2
>>> intersection_size([], [1, 2])
0
>>> intersection_size(['a', 'b'], ['b', 'c'])
1
5 points ~8 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Convert one list to a set to make membership checks fast.
Then count how many elements of the other list are in that set.
Remember to count each distinct common element only once — using a set for the result will help.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.