Go Down

Topic: Compare Two Arrays of Different Lengths and Return the Common Values (Read 1 time) previous topic - next topic

tx0rx0

I'd like to define a function that compares two arrays with differing lengths and returns the values common to both arrays. Can anyone help me with how that will be written? Thanks.

GoForSmoke

How would you do it if it was two lists of letters or numbers on paper?

Let's say letters and these are the lists:

A B C D A B C D

A A C D C B A B C A D D D

Would you start with the first letter of the first list and find a match in the second list?
And if you do, will you record that?
Will you remove the matched letter in the second list so it doesn't get matched again?
And then on to the next letter in the first list? And the next till it's done?

Just how... sorry but I won't write your code. But I will suggest modeling the actions and playing with for-next and while loops and keeping information in variables before deciding what to write.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Quick5pnt0

I don't know if there is an easier way but with two for loops, one inside the other, you can compare the contents of one array with another.

Something like this:
For loop i
  For loop j
      If i equals j
          do something
      }
  }
}

That is pseudo code but you get the idea.

     

marco_c

Just to add another question:

Are the lists sorted in order? That would allow the matching to be more efficient
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

PaulS

Do you mean when the ith element in one array matches the ith element in the other array, or do you mean when the ith element in one array matched any element in the other array?

Go Up