numSame
Category: Arrays
Author: Stuart Reges
Book Chapter: 7.2
Problem: numSame
Write a static method numSame that compares two sorted arrays of integers, returning the number of values that are the same between the two. For example, if two sorted arrays store the following values: list1: (2, 4, 6, 20) list2: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) Then the method call: numSame(list1, list2) would return 3, because three of the numbers are the same in each list (i.e., 2, 4 and 6). You should get the same result from this call: numSame(list2, list1) There may be duplicates in either list. Any given number in a list can match at most once, so a duplicate in one list can match a duplicate in another, but if one list has fewer duplicates than the other, then only that smaller number of duplicates can match. For example, if the lists store: list1: (1, 1, 1, 1, 2, 2, 3, 4, 5, 8, 12) list2: (1, 1, 3, 3, 3, 3, 6, 6, 7, 7, 8, 9, 10, 11) Then the method call: numSame(list1, list2) would return 4, because 4 of the values match (two of the 1's, one 3 and 8). Notice that only two of the 1's in list1 match because list2 has only two 1's. Also notice that even though list2 has four 3's, only one ends up matching because list1 has only one 3. As before, the method should return the same result if the order of the lists is reversed in the call. Remember that the numbers in the two arrays will be in sorted (nondecreasing) order. You must solve this problem using the two arrays that are passed as parameters. You are not allowed to use additional arrays or another structured object like an ArrayList. You also may not modify the arrays that are passed as parameters. You can, however, receive up to 4 of the 10 points if your solution works for lists that have no duplicates. Write your solution to numSame below.