Start learning free

LESSON 233 OF 673 · SETS

Common Count in Python: Counting Shared Values with Sets

Practice Read the full explanation

Given two lists of integers, a and b, return how many different values appear in both of them.

A value counts only once, however many times it appears in a or in b.

 

Example 1:

Input: a = [1, 2, 3, 4], b = [3, 4, 5]
Output: 2
Explanation: 3 and 4 are in both. 1 and 2 are only in a, and 5 is only in b.

Example 2:

Input: a = [7, 7, 8], b = [7, 9, 7]
Output: 1
Explanation: 7 is the only value in both. It appears twice in each, but it counts once.

Example 3:

Input: a = [1, 2], b = [3, 4]
Output: 0
Explanation: No value is in both.

 

Constraints:

  • a and b each have at least one number.
  • Every number is between -109 and 109.
STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · SETS

    Common Count in Python, explained

    Problem overview

    The method commonCount receives two lists of integers, a and b, and returns how many different values appear in both. A value counts once, however often it shows up in either list.

    With a = [1, 2, 3, 4] and b = [3, 4, 5], the values 3 and 4 are in both lists, so the answer is 2. With a = [7, 7, 8] and b = [7, 9, 7], the value 7 appears twice in each list, but it still counts once: the answer is 1. With a = [1, 2] and b = [3, 4] nothing is shared, so the answer is 0.

    Approach: a set for lookups, a set for the answer

    Checking whether a value is in a list means scanning the list. Checking whether a value is in a set is fast, because a set is built for membership tests. So the first move is to turn a into a set. Then every value of b can be checked against it quickly.

    The second set, common, collects the values found in both lists. A set ignores repeats: adding 7 a second time changes nothing. That is why the answer is its size and not a running counter. A counter would add 1 for each appearance of 7 in b; the set holds 7 once.

    Step by step

    1. Build setA with set(a). Duplicates in a disappear here.
    2. Create an empty set named common with set(). Writing {} would make an empty dictionary instead, so an empty set needs the function call.
    3. Loop over b, one value at a time, called num.
    4. Test membership. If num in setA is true, the value is in both lists, so add it to common.
    5. Return len(common) after the loop, so every value of b has been checked first.

    The solution in Python

    class Solution:
        def commonCount(self, a: List[int], b: List[int]) -> int:
            setA = set(a)
            common = set()
            for num in b:
                if num in setA:
                    common.add(num)
            return len(common)
    

    A few Python details:

    • set(a) copies. The list a is not changed. The set keeps only distinct values and has no order, so don't expect to read its items back in the list's order.
    • in means different things for lists and sets. On a list it scans item by item; on a set it is a direct lookup. The result is the same, the cost is not.
    • add is safe to repeat. Adding a value already in the set does nothing and raises no error.

    Python also has an operator for exactly this job: set(a) & set(b) is the set of values in both. Its length gives the same answer, so len(set(a) & set(b)) is a one-line version. The loop version shows what that operator does.

    Edge cases and common mistakes

    • Counting appearances instead of values. Looping over b and adding 1 for each match gives 2 for a = [7, 7, 8], b = [7, 9, 7], because 7 occurs twice in b. The expected answer is 1.
    • Skipping the set. Testing num in a against the list works, but num in a scans the whole list for every value of b, so large inputs get slow.
    • Negative numbers and zero. They are ordinary values: a = [-1, 0] and b = [0, -1, 1] share two values, so the answer is 2.
    • One value repeated many times. a = [5] and b = [5, 5, 5] gives 1.

    Testing

    • a = [1, 2, 3, 4], b = [3, 4, 5] gives 2.
    • a = [7, 7, 8], b = [7, 9, 7] gives 1.
    • a = [1, 2], b = [3, 4] gives 0.
    • a = [5], b = [5, 5, 5] gives 1.
    • a = [-1, 0], b = [0, -1, 1] gives 2.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free