Start learning free

LESSON 236 OF 289 · SETS

Common Count in TypeScript: Counting Shared Values with Set

Practice Read the full explanation

Given two arrays 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

    TYPESCRIPT · SETS

    Common Count in TypeScript, explained

    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

    1. Build setA from a with new Set(a). The constructor takes any iterable, so an array works directly, and duplicates disappear as it is built.
    2. Create the empty set common. It collects the values found in both arrays. A plain counter would count a repeated value in b several times, which is the mistake the set avoids.
    3. Loop over b with for...of. Each value of b is checked once.
    4. Test and add. If setA.has(num) is true, the value is in a and, since the loop found it, in b. common.add(num) records it.
    5. Return common.size. size is 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, add and size. has(x) tests membership, add(x) inserts (and does nothing if x is already there), and size counts the distinct values.
    • Numbers compare by value. A set of numbers treats 7 and 7 as the same element, and -5 and 5 as 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 for a = [7, 7, 8] and b = [7, 9, 7] it would answer 2 instead of 1. The set common prevents this.
    • Using setA.size for the answer. That is the number of distinct values in a, not the shared ones.
    • No overlap. a = [1, 2] and b = [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] gives 2.
    • a = [7, 7, 8], b = [7, 9, 7] gives 1.
    • a = [1, 2], b = [3, 4] gives 0.
    • a = [-5, 0, 5], b = [5, -5, 5, -5] gives 2.
    • a = [1], b = [1] gives 1.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free