# Computer Programming



## diatron5 (Feb 3, 2011)

Anyone on here do this?
It's my major and I need help with something.
I could just look up the answer and submit somebody elses work but I think I'm really close to solving this.
Here's what I need to do
CodingBat Java AP-1 commonTwo
"Start with two arrays of strings, a and b, each in alphabetical order, possibly with duplicates. Return the count of the number of strings which appear in both arrays. The best "linear" solution makes a single pass over both arrays, taking advantage of the fact that they are in alphabetical order. 

commonTwo({"a", "c", "x"}, {"b", "c", "d", "x"}) &#8594; 2
commonTwo({"a", "c", "x"}, {"a", "b", "c", "x", "z"}) &#8594; 3
commonTwo({"a", "b", "c"}, {"a", "b", "c"}) &#8594; 3"

Here's my code:

```
public int commonTwo(String[] a, String[] b) 
{
    //outer loop for first array
    //inner loop compares all elements in second array with first
    //track matches
    //return matches
    int matches = 0;
    
    for(int x = 0; x < a.length; x++)
    {
        for(int y = 0; y < b.length; y++)
        {
            if(a[x].equals(b[y]))
            {
                matches += 1;
            }
        }
    }
    
    return matches;
}
```

Here's a screenshot of my errors:






Sorry if noone does this and reading this thread was just a complete waste of time!


----------



## Thep (Feb 3, 2011)

//Can't help you but I'm taking my first semester of programming for engineering. Cannot wait till I can has teh java


----------



## troyguitar (Feb 3, 2011)

I'm not sure how much help you want exactly, but here are 2 things:

1 - You're not taking duplicates in either array into consideration. If a[x] == a[x-1] then you will be checking for (and counting) that element in b twice. Similarly if b[y] == b[y-1] it will be counted as an additional common element. Solving those issues will give you a function which returns the right answer, but is inefficient. See 2 for explanation

2 - You're not utilizing the fact that they are both in alphabetical order. Running over the entire second array for each character in the first is unnecessary - you certainly won't find W before R.


----------



## diatron5 (Feb 3, 2011)

thank you. I got it.


----------



## thesimo (Feb 4, 2011)

show us your solution.


----------



## Agone (Feb 8, 2011)

The code below passes the test. 

There is probably an other/more efficient/cleaner way to do it... (I enjoy writting ugly code when it's not for work )

Edit: Thanks for that web site! 



```
public int commonTwo(String[] a, String[] b) {
   
   int matches = 0;
   int x,y;
   int lastMatchIdx = 0; 

   for(x = 0; x < a.length; x++)
    {

        //ignore duplicate values in the array a[]
        if(x+1 < a.length && a[x].equals(a[x+1]) )
        { 
          continue;
        }
        
        //start at last match index (alphabetical order)
        
        for(y = lastMatchIdx ; y < b.length; y++)
        {
            if(a[x].equals(b[y]))
            {
                matches ++;
                lastMatchIdx = y; 
                break; //dont look any further.
            }
        }
    }
    return matches;
}
```


----------



## troyguitar (Feb 8, 2011)

I did it a little bit differently but I think both are about the same...


```
public int commonTwo(String[] a, String[] b) {
	int x= 0,y = 0, matches = 0;
	while(x<=a.length && y<= b.length){
		while(x<a.length && a[x]==a[x+1])
			x++;
		while(y<b.length && b[y]==b[y+1])
			y++;
		if(a[x]==b[y]){
			matches++;
			x++;
			y++;
			}
		else if(a[x]<b[y])
			x++;
		else y++;
		}
	return matches;
	}
}
```


----------

