# Compare Two Arrays of Different Lengths and Return the Common Values

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.

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 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.

Just to add another question:

Are the lists sorted in order? That would allow the matching to be more efficient

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?

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.

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.

So I’ve come up with something like this

``````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

``````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

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.

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.

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.