# Comparing all elements of an array

For a program that I am writing for fun (one that finds the Highest Common Factor and the Lowest Common Multiple for you); I've come across some difficulty.

I have two arrays that contain 14 numbers. To find the Lowest Common Multiple of all the numbers, I need to compare every element in each array. So far I've got this test:

for(int i = 0; i < C_I_14; i++) { for(int j = 0; j < C_I_14; j++) { if(array[i] == arr[j]) { tesst[i] = array[i]; } } }

(where C_I_14 = 14)

The thing is, there are endless amounts of things that could go wrong with: tesst[i] = array[i]

So, can anyone help me sort out my little algorithm?

## Answers

Sort each of your input arrays, then get the intersection using std::set_intersection.

If the ordering matters, you will find

quite useful

Otherwise, look at

- std::sort (!! important) followed by
- std::set_intersection