Problem overview
The method commonCount receives two arrays of integers, 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 is the only shared value and it counts once even though it appears twice on each side. With a = [1, 2] and b = [3, 4] nothing is shared, so the answer is 0.
Approach
Two ideas are needed: fast membership checks and counting each value once. A Set gives both. Asking an array whether it contains a value means scanning it, but asking a set is fast on average, because a set is a hash table. And a set never stores the same value twice, so inserting a value that is already there changes nothing.
The plan: put the values of a into a set, then walk through b. Each value of b that is in that set belongs to both arrays, so it goes into a second set, common. The size of common is the answer. If 7 appears many times in b, it is inserted many times but stored once.
Step by step
- Build a set from
a.Set(a)turns the array into a set, dropping duplicates. The constant is calledsetA, andletis enough because it never changes. - Create an empty result set.
Set<Int>()needs its element type written out, since there is nothing to infer it from. It is avarbecause values are added to it. - Loop over
b. Each value is callednum. - Test and insert. If
setA.contains(num)is true, the value is in both arrays, socommon.insert(num). - Return
common.count. The count of a set is the number of distinct values in it.
The solution in Swift
class Solution {
func commonCount(_ a: [Int], _ b: [Int]) -> Int {
let setA = Set(a)
var common = Set<Int>()
for num in b {
if setA.contains(num) {
common.insert(num)
}
}
return common.count
}
}
Swift details worth knowing:
Set(a)works becauseIntisHashable. The element type is inferred from the array, so the result is aSet<Int>.insertis safe to repeat. Inserting a value that is already in the set leaves the set unchanged, so no check is needed before it._in the signature means callers pass the arguments without labels:commonCount([1, 2], [2, 3]).- The result of
insertis a tuple saying whether the value was new. Swift marks it as discardable, so ignoring it produces no warning.
Complexity
Time is O(n + m) on average, where n and m are the lengths of a and b: building setA visits every value of a once, and the loop makes one set lookup per value of b. Space is O(n + m) in the worst case, for the two sets.
Edge cases and common mistakes
- Counting matches instead of distinct values. A counter that goes up for every value of
bfound inareturns 2 fora = [7, 7, 8],b = [7, 9, 7]. The expected answer is 1, which is whycommonis a set. - Duplicates in
aonly. They disappear whenSet(a)is built, so they cause no trouble. - Repeated values in
bonly.a = [5, 5, 5]andb = [5]give 1. - No overlap. The loop never inserts anything, and
common.countis 0.
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, 5, 5],b = [5]gives 1.
