This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Loop Condition Question

Marvin Porte
Greenhorn
Posts: 11
Problem:
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"}) → 2
commonTwo({"a", "c", "x"}, {"a", "b", "c", "x", "z"}) → 3
commonTwo({"a", "b", "c"}, {"a", "b", "c"}) → 3

My Solution:

The code is running fine and correct for all the tests but I'm in doubt at some points.

My Question:
i really can't determine the looping condition on how many times I'm going to loop here since there's no specific given and you can't determine somehow (I think..).
so what i did was get the max length (a.length*b.length) then use it as looping condition (but i know that it will not reach to that point. i just make it to make sure that it will loop all the conditions).
then i just created a break point on the loop where counters reach their max value (array length).
is it legal? i mean is it an acceptable code as a programmer? it works fine but i don't know if it's acceptable or a good practice. and can you give me any idea to solve this one if you have an idea for looping condition.

Campbell Ritchie
Sheriff
Posts: 48954
60
Don’t know. Don’t know. Don’t know.

You cannot work out an algorithm on screen. You need to work it out on paper. Write down on paper how you would go through those two arrays once each and check whether there are common elements. Remember the question says the two arrays are pre‑sorted in alphabetical order.
Is this another codingbat question? If so please supply link.

Steve Myers
Ranch Hand
Posts: 47
Marvin Porte wrote:Problem:
My Question:
i really can't determine the looping condition on how many times I'm going to loop here since there's no specific given and you can't determine somehow (I think..).
so what i did was get the max length (a.length*b.length) then use it as looping condition (but i know that it will not reach to that point. i just make it to make sure that it will loop all the conditions).
then i just created a break point on the loop where counters reach their max value (array length).
is it legal? i mean is it an acceptable code as a programmer? it works fine but i don't know if it's acceptable or a good practice. and can you give me any idea to solve this one if you have an idea for looping condition.

A while loop with no break; statement makes a lot more sense for this for code clarity.

if you wanted to leave the break statement (perhaps there were two separate exit points from the loop):

In your version the for loop is not needed, it's not an exit point, the value of i is not used for anything and its just confusing.

Junilu Lacar
Bartender
Posts: 7466
50
Marvin Porte wrote:
is it legal? i mean is it an acceptable code as a programmer? it works fine but i don't know if it's acceptable or a good practice. and can you give me any idea to solve this one if you have an idea for looping condition.

Legal / acceptable? For a beginner, it's OK. It's not perfect though.

You already have a pretty good loop condition in that first if-statement. Use a variation of it in a while-loop instead.

This line of code is not good

Never do that unless you are checking for reference equality (do temp and a[counterA] refer to the same instance?) -- in this case you want to check the values, not the reference. Use equals() instead.

counterA and counterB are not good names -- they are not counters, they are indices. Make your variable names reflect their purpose and usage. If they don't, your code just gets confusing to read.

Junilu Lacar
Bartender
Posts: 7466
50
Also, run through your loop manually, with pencil and paper and see if temp is really useful. I don't see how it is.

Marvin Porte
Greenhorn
Posts: 11
@Campbell
Here's the link of the problem. http://codingbat.com/prob/p100369
Yeah I check the algorithm on paper and it works fine but as Sir steve said, I used "useless loop condition with i and it's confusing" and I agree with him. It's just like a while(true) with breaking point inside, relying only on that breaking point to stop the loop without using the loop condition.

@Steve
Thanks sir! Thank you very much I really get your point. Thanks a lot.

@Junilu
Alright sir thank you for pointing out my mistakes.
By the way, I used String temp so that I could temporarily store the last value. Because when I created the code without temp, the program still counts another common instance say for example Array1 is {a,a,b,b,d} and Array2 is {a,a,b,b,c}, the program still counts another instance of a and b which will output 4, but the correct answer is only 2. I used temp to disregard the common instance if the letter is already counted.

Thanks everyone for all your help!

Abhishk Singh
Ranch Hand
Posts: 50
One solution can be as follows:
Convert both the arrays to sets one by one.
Then iterate over the smaller set and check if the element in the smaller set is contained in the larger one. Whenever a match is found, the counter can be incremented by 1.