I was asked to write down a Java function sharedStr that by given 2 sorted arrays of Strings, returns the number of Strings that appea
int x = 0;
int i= 0;
int j = 0;
while(i != list1.length && j != list2.length){
int v = list1[i].compareTo(list2[j]);
if (v == 0){
x++;i++;j++;
}else if (v < 0){
i++;
} else {
j++;
}
}
return x;
This makes the straightforward assumption that the strings are sorted using String.compareTo which would make sense.