easy +8 pts

Intersection two lists length

Count the number of distinct elements common to two lists.

Write a function `intersection_length(list1, list2)` that takes two lists (each may contain duplicates) and returns the count of **distinct** elements that appear in both lists. The order of elements does not matter. For example, if `list1 = [1, 2, 2, 3]` and `list2 = [2, 3, 4]`, the common distinct elements are `{2, 3}`, so the answer is `2`. - The input lists can be empty. - Elements can be of any hashable type (integers, strings, etc.). - Your function should work efficiently for large lists.

Constraints

- 0 <= len(list1), len(list2) <= 10^5 - Elements are hashable (e.g., integers, strings). - Complexity: O(n + m) time, O(n) space (where n and m are the lengths of the lists).

Example

```python
>>> intersection_length([1, 2, 2, 3], [2, 3, 4])
2
>>> intersection_length([1, 1, 1], [2, 2])
0
>>> intersection_length([], [1, 2])
0
>>> intersection_length(['a', 'b'], ['b', 'c'])
1
```
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Consider using a set to store the unique elements of one list, then check membership for the other.
Remember to count each common element only once, even if duplicates exist.
You can use the set intersection operation or a loop with a set.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.