Problem overview
CommonCount receives two int[] arrays, 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.
With a = [1, 2, 3, 4] and b = [3, 4, 5] the answer is 2: the values 3 and 4 are in both. With a = [7, 7, 8] and b = [7, 9, 7] the answer is 1, because 7 appears twice on each side but is still one value. 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
A HashSet<int> stores each value at most once and answers "is this value in here?" quickly. That gives two jobs for sets:
- Put the values of
ain a set. Checking whether a value ofbalso appears inabecomes one fastContainscall, instead of a loop over all ofa. - Collect the shared values in a second set. Adding a value that is already in a set does nothing, so repeated values in
bcan't be counted twice. A plain counter would count them twice, which is the mistake this lesson is built to avoid.
Step by step
- Build
setAwithnew HashSet<int>(a). The constructor takes any collection of ints and keeps one copy of each value, so the duplicates inadisappear here. - Create an empty set named
common. It will hold the values found in both arrays. - Loop over
bwithforeach (int num in b). Every value ofbgets one check againstsetA. - Test
setA.Contains(num). When it is true,numis inb(the loop found it) and ina(the set says so), socommon.Add(num). - Return
common.Count. On aHashSet,Countis a property, not a method, so there are no parentheses.
The solution in C#
class Solution {
public int CommonCount(int[] a, int[] b) {
HashSet<int> setA = new HashSet<int>(a);
HashSet<int> common = new HashSet<int>();
foreach (int num in b) {
if (setA.Contains(num)) {
common.Add(num);
}
}
return common.Count;
}
}
A few C# details:
HashSet<T>lives inSystem.Collections.Generic. In a file of your own, addusing System.Collections.Generic;at the top.Addreturns abool:trueif the value was new,falseif the set already had it. Starting from the set {1, 2, 3},Add(2)givesFalseandAdd(9)givesTrue, and the set then has 4 values. This lesson ignores the return value, because adding an existing value is harmless.- Values up to a billion in size fit in an
int(largest value 2,147,483,647), and the code never does arithmetic on them, so overflow can't happen.
Complexity
Building setA takes one pass over a, and the loop is one pass over b with a fast lookup per value, so the time is O(n + m) for arrays of length n and m. The two sets use O(n + m) extra memory at most. Comparing every pair of values with two nested loops would take O(n × m).
Edge cases and common mistakes
- Counting every match. Incrementing a counter inside the loop gives 2 for
a = [7],b = [7, 7]when the answer is 1. Adding to a set avoids that. - Duplicates in
a. They are harmless, because the set built fromaalready merges them. - No shared values.
commonstays empty andCountis 0. - Negative numbers and extremes.
a = [-1000000000, 1000000000]againstb = [1000000000, -1000000000, 0]returns 2. Sets treat negative values like any other.
Testing
Inputs worth trying, with the results the solution returns:
a | b | Result |
|---|---|---|
[1, 2, 3, 4] | [3, 4, 5] | 2 |
[7, 7, 8] | [7, 9, 7] | 1 |
[1, 2] | [3, 4] | 0 |
[5] | [5, 5, 5] | 1 |
