A problem, from the top of my head.

6 views
Skip to first unread message

Jan Monterrubio

unread,
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

unread,
May 5, 2011, 11:00:57 PM5/5/11
to cs251-...@googlegroups.com
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

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

Kalen Miller

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

Jan Monterrubio

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

Kalen Miller

unread,
May 5, 2011, 11:39:07 PM5/5/11
to cs251-...@googlegroups.com
for
(String a : arr1){
    for(String b : arr2){
        if(a.equals(b))count++;
--
Gunner
Reply all
Reply to author
Forward
0 new messages