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
- Build
setAwithset(a). Duplicates inadisappear here. - Create an empty set named
commonwithset(). Writing{}would make an empty dictionary instead, so an empty set needs the function call. - Loop over
b, one value at a time, callednum. - Test membership. If
num in setAis true, the value is in both lists, so add it tocommon. - Return
len(common)after the loop, so every value ofbhas 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 listais 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.inmeans 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.addis 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
band adding 1 for each match gives 2 fora = [7, 7, 8],b = [7, 9, 7], because 7 occurs twice inb. The expected answer is 1. - Skipping the set. Testing
num in aagainst the list works, butnum in ascans the whole list for every value ofb, so large inputs get slow. - Negative numbers and zero. They are ordinary values:
a = [-1, 0]andb = [0, -1, 1]share two values, so the answer is 2. - One value repeated many times.
a = [5]andb = [5, 5, 5]gives 1.
Testing
a = [1, 2, 3, 4],b = [3, 4, 5]gives2.a = [7, 7, 8],b = [7, 9, 7]gives1.a = [1, 2],b = [3, 4]gives0.a = [5],b = [5, 5, 5]gives1.a = [-1, 0],b = [0, -1, 1]gives2.
