#K4736. Count Common Elements
Count Common Elements
Count Common Elements
Given two arrays of integers, your task is to count the number of common elements taking into account their multiplicities. In other words, for any integer that appears in both arrays, you should add the minimum number of times it appears in either array.
For example, if the first array is [1, 2, 2, 3, 4] and the second array is [2, 2, 3, 3], the common elements are two occurrences of 2 and one occurrence of 3. Thus, the answer is 3.
The problem can be formulated mathematically as:
\( \text{result} = \sum_{x \in S} \min(\text{count}_{arr1}(x),\, \text{count}_{arr2}(x)) \)
where \(S\) is the set of integers that occur in both arrays.
inputFormat
The input consists of two lines:
- The first line contains the elements of the first array separated by spaces. If the array is empty, the line will be empty.
- The second line contains the elements of the second array in the same format.
outputFormat
Output a single integer representing the total count of common elements between the two arrays, taking into account the multiplicities.
## sample1 2 2 3 4
2 2 3 3
3