Go Down

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

#### tx0rx0

##### Jan 25, 2013, 04:41 am
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

#1
##### Jan 25, 2013, 05:06 am
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.

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### Quick5pnt0

#2
##### Jan 25, 2013, 05:25 am
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

#3
##### Jan 25, 2013, 05:31 am

Are the lists sorted in order? That would allow the matching to be more efficient
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### PaulS

#4
##### Jan 25, 2013, 07:07 am
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?

#### GoForSmoke

#5
##### Jan 25, 2013, 07:11 am
Each array in ascending or descending order. Because then you'd know when to stop iterating the match search.

Unless I miss my guess, elements of both arrays may or may not have matches.

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### marco_c

#6
##### Jan 25, 2013, 09:13 am
Correct guess. I think that if both lists are sorted within themselves it is possible to do the task with a single pass through the lists.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### tx0rx0

#7
##### Jan 25, 2013, 07:55 pmLast Edit: Jan 25, 2013, 07:59 pm by tx0rx0 Reason: 1
So I've come up with something like this
Code: [Select]
`int  common_elements(int a[], int b[]){  a.sort();b.sort();n, o = 0, 0;common = [];while n < len(a) and 0 < len(b):if a[n] == b[o]{  common.append(a[i]);  n += 1;  o += 1;}if (a[n] < b[o]){  n += 1;else{  o += 1;}}`

I think I've got something wrong with the types as it fails to compile with this error. Anyone wanna point out any errors, I'm not that experienced with C code. Thanks
Code: [Select]
`reguest for member 'sort' in 'a'. which is of non-class type 'int*'`

A little more data on the input. One string contains 7 fixed numbers and the other contains three fixed numbers. I want to find the common numbers in the two arrays in order to iterate though a couple sets of color and pinout data

#### KeithRB

#8
##### Jan 25, 2013, 08:00 pm
Arrays don't have an automatic sorting operator, you must use qsort() or write your own.

This is not C++. You will have to rework a lot!

common = []

you need parens for the while statement and no colon.

you need parens in your if statement

and so on.

#### PeterH

#9
##### Jan 25, 2013, 10:27 pm
Unless you have a particular need for this to run as fast as possible, for arrays that small I wouldn't bother sorting them and would just brute-force compare the unsorted arrays element by element.

for each element in left array
for each element in right array
if left == right
append to results

If you want the results to be unique and the input arrays are not unique then I'd implement the append operation in a function that checked the new result against the values already in the result array.

I only provide help via the forum - please do not contact me for private consultancy.

#### GoForSmoke

#10
##### Jan 25, 2013, 10:41 pm
This is C++ but you have to use a container class (hope you've got the RAM) to get that sort.
But honestly, it's more work to sort the arrays than to simply find matches.

Also OP, learn to use pointers and pass those to your functions. Pointers are like using web links except for RAM. They're just address holders with names that know the size variable they point to. You ++ a pointer, it points to the next variable in your array. The array name is itself a pointer to the start of the array.

byte  array[ 20 ];
byte *a = array; // now *a points to the beginning of array[ 20 ]

There's a very few steps to learn that you can pick up between examples and explanations.

Work the steps out with pencil and paper or keyboard and editor. Understanding what you're doing will in the end show you how simple the basis is and no magic pill class function will ever do that. In fact, magic pill functions is a great way to **never learn** how simple these things work which means for the next and next problem the answer will be finding more magic pills.

C is really very simple at heart. So is any alphabet. What you do with either can make the complexity. You don't need to include all of Crime and Punishment to refer to beating a dead horse.
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up

Please enter a valid email to subscribe