tags:

views:

64

answers:

4

How can I create a method that recieve two arrays as parameters and return an array filled with the items that were in both arrays?

Input (Array1 passed in method): ["Lisa", "George", "Mario"] Input (Array2 passed in method): ["Luigi", "Susan", "Lisa"]

Method should return: ["Lisa"]

I cannot use any built in methods so I have to build my own algorithm, but I'm stuck for the past 2 hours. How can I achieve this in Java?

Edit: Christ on a candle stick. It's not for homework. I'm just real shitty at algorithms. Especially ones as basic as this, and especially on a foreign language I've never used. :P

+3  A: 

One quick way is the following algorithm:

For each item in list1, add it to a dictionary.
For each item in list2, check if it exists in dictionary, 
if item exists, add it to list3
else continue.
return list3.
Alan
@Alan, I'd recommend that you replace "dictionary" with "set" (assuming that we're not always talking about words).
Bob Cross
Thanks @Bob, however this was a more generalized algorithm, not pertaining to any language.
Alan
@Alan: "set" is a more general term than "dictionary". Besides, a dictionary is a mapping from keys to values, and you don't need a mapping ... just a set.
Stephen C
@Alan: "set" is a more (programming) language independent term than "dictionary". Besides, a dictionary is a mapping from keys to values, and you don't need a mapping ... just a set.
Stephen C
@Stephen C: once is enough. You almost repeated the same thing word for word.
duffymo
+2  A: 

Brute force.

Loop over the first array. Inside that loop, have another over the second array. Compare entries from both, adding them to a new array if you get a match.

duffymo
Brute force is O(N*2). A solution using sets is O(N) or O(NlogN) depending on which Set implementation you use (and on certain other assumptions).
Stephen C
Agreed, but he said he couldn't use built-in functions, so I assumed that precluding using java.util Collections. And certainly for the small example cases he presented the Big-Oh notation is irrelevant. Alan's answer is pretty much the same as mine - two nested loops - and it's been voted up three times.
duffymo
+1  A: 
    [] result = ...

    foreach( itemA in a ) {
        foreach(  iteamB in b ) {
             if( itemB == itemA ) { // if they are in both places
                   r[] = itemB // append it to the result
             }
         }
    }
    return result;
OscarRyz
+4  A: 

How about:

private static String[] findCommon(final String[] array1,
        final String[] array2) {
    final Set<String> common = new HashSet<String>();
    common.addAll(Arrays.asList(array1));
    common.retainAll(Arrays.asList(array2));
    return common.toArray(new String[0]);
}
toolkit
+1 for retainAll(). I'd never noticed that....
Brian Agnew
"...cannot use any built in methods..." - doesn't this preclude using the java.util Collections?
duffymo
Hadn't spotted that duffymo. Though, since it isn't homework, I don't understand why this constraint would exist ;-)
toolkit
@duffymo, my recommendation for the not-homework would be to look up the definition of the built-in method and then implement that. toolkit's implementation is sufficient as an algorithm sketch; now the OP just needs to fill in the remaining blanks. On the not-homework....
Bob Cross