# A problem, from the top of my head.

6 views

### Jan Monterrubio

May 5, 2011, 10:28:43 PM5/5/11
to CS251 S11 Group
the method, duplicateStrings takes two String arrays.

It compares how many of the strings in the first array are equal to the strings in the second array and
counts the number of strings that are in both.

public int duplicateStrings(String[] arr1, String[] arr2);

-> write this method

For

String[] arr1 = {"ABC", "DEF", "GH", "IJ"};

String[] arr2 = {"AB", "DEF", "IJ", "GH"};

I got 3.

Your method should be shorter than 10 lines.

### Kalen Miller

May 5, 2011, 11:00:57 PM5/5/11
Think this works:

public

static int duplicateStrings(String[] arr1, String[] arr2){

int count = 0;

if(arr1.length < arr2.length)duplicateStrings(arr2,arr1);

for(int i = 0; i<arr1.length; i++){

for(int j = 0; j<arr2.length; j++){

if(arr1[i].equals(arr2[j]))count++;

}

}

return count;

}

--
Gunner

### Kalen Miller

May 5, 2011, 11:07:30 PM5/5/11
Not sure why I decided to add if(arr1.length < arr2.length)duplicateStrings(arr2,arr1);  overthinking again...
--
Gunner

### Kalen Miller

May 5, 2011, 11:15:21 PM5/5/11
"counts the number of strings that are in both": System.
out.println(arr1.length+arr2.length);
--
Gunner

### Jan Monterrubio

May 5, 2011, 11:24:20 PM5/5/11
to CS251 S11 Group

### Kalen Miller

May 5, 2011, 11:39:07 PM5/5/11
for
(String a : arr1){
for(String b : arr2){
if(a.equals(b))count++;
--
Gunner