Write a recursive function named matchCount
that accepts two references to arrays of integers as parameters and that returns the number of elements that match between them.
Two elements match if they occur at the same index in both lists and have equal values.
For example, given the two arrays shown below, the call of matchCount(v1, v2)
would compare as follows:
v1: [2, 5, 0, 3, 8, 9, 1, 1, 0, 7]
| | | | | | |
v2: [2, 5, 3, 0, 8, 4, 1]
The function should return 4 in this case because 4 of these pairs match (2-2, 5-5, 8-8, and 1-1).
If either array is empty, by definition it has no matches with the other array, so your function should return 0.
Constraints:
- Do not use any loops; you must use recursion.
- Do not declare any global variables.
- Do not use a string or array other than those passed as parameters to your function.
- When your code is done running, the two arrays should have the same contents as when the call began.
- You may define other "helper" functions if you like; they are subject to these same constraints.
- Your solution should run in no worse than O(N) time, where N is the number of elements in the arrays.