Problem overview
The method commonCount receives two arrays of numbers, a and b, and returns how many different values appear in both. A value counts once, however many times it shows up in either array.
For a = [1, 2, 3, 4] and b = [3, 4, 5], the values 3 and 4 are in both arrays, so the answer is 2. For a = [7, 7, 8] and b = [7, 9, 7], the value 7 appears twice in each array but counts once, so the answer is 1.
Approach: two sets and a membership test
The question "is this value also in the other array?" is slow to answer on an array, because includes has to look at the elements one by one. A Set answers it with has, which is fast, and a set also ignores duplicates: adding a value that is already inside changes nothing. Both properties are what this problem needs.
The plan: put the values of a in a set, walk through b, and every time a value of b is in that set, record it in a second set. The size of the second set is the answer. Because it is a set, a value found three times in b is still recorded once.
Step by step
- Build
setAfromawithnew Set(a). The constructor takes any iterable, so an array works directly, and duplicates disappear as it is built. - Create the empty set
common. It collects the values found in both arrays. A plain counter would count a repeated value inbseveral times, which is the mistake the set avoids. - Loop over
bwithfor...of. Each value ofbis checked once. - Test and add. If
setA.has(num)is true, the value is inaand, since the loop found it, inb.common.add(num)records it. - Return
common.size.sizeis a property, not a method, so there are no parentheses.
The solution in TypeScript
class Solution {
commonCount(a: number[], b: number[]): number {
let setA: Set<number> = new Set(a);
let common: Set<number> = new Set();
for (let num of b) {
if (setA.has(num)) {
common.add(num);
}
}
return common.size;
}
}
A few details are worth knowing:
- Types are erased. The annotations help the compiler and the editor, but after compiling, only
new Set(a)and the rest of the JavaScript run. has,addandsize.has(x)tests membership,add(x)inserts (and does nothing ifxis already there), andsizecounts the distinct values.- Numbers compare by value. A set of numbers treats
7and7as the same element, and-5and5as different ones.
Complexity
Time: O(n + m), where n is the length of a and m the length of b. Building setA visits each element of a once, and the loop visits each element of b once, with has and add taking constant time on average. Checking every value of b against the whole array a instead would take O(n * m).
Space: O(n + m) in the worst case, for the two sets.
Edge cases and common mistakes
- Counting repeats. A counter that goes up by one for each match counts every appearance in
b, so fora = [7, 7, 8]andb = [7, 9, 7]it would answer 2 instead of 1. The setcommonprevents this. - Using
setA.sizefor the answer. That is the number of distinct values ina, not the shared ones. - No overlap.
a = [1, 2]andb = [3, 4]returns 0. - Negative numbers and zero. They are ordinary values:
[-5, 0, 5]and[5, -5, 5, -5]share two values and return 2.
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, 0, 5],b = [5, -5, 5, -5]gives2.a = [1],b = [1]gives1.
