Compare two Arrays

aikea

Registered User.
Local time
Yesterday, 16:09
Joined
Aug 4, 2007
Messages
58
I was trying to compare two arrays, to identify whether one contains another

Both arrays are sorted, for example:

Array 1: {1, 2, 3}
Array 2: {1, 1, 2, 2, 3, 3}

There are two copys of Array 1 in Arrary 2.

I wonder how could I make the comparation possible, for example:

I can built two functions:

Contain(Array2, Array1) will return "True"
ContainedCopy(Array2, Array1) will return "2"
 
Loop out the arrays to create one string (Place a "," to sperate the fields).

Then compare the two strings.
 
Well, If I combine array1 {1,2,3} I will get "123"
and If I combine array2 {1,1,2,2,3,3} I will get "112233"

I am not sure how you can compare this two, they are two different strings.
 
checkout the Instr() function but im not sure it would match perfectly
 
Aikea, can you clarify if array2 contains data from other arrays as well
ie does the following ever occur

Array1A: {1, 2, 4 }
Array1B; {1, 2, 3, 4}
Array 1: {1, 2, 3}
Array 2: {1, 1, 2, 2, 3, 4}

In this case which arrays does Array 2 contain?
 
Aikea, can you clarify if array2 contains data from other arrays as well
ie does the following ever occur

Array1A: {1, 2, 4 }
Array1B; {1, 2, 3, 4}
Array 1: {1, 2, 3}
Array 2: {1, 1, 2, 2, 3, 4}

In this case which arrays does Array 2 contain?

Thank you all for your reply

Both arrays are contains random numbers, but they can be sorted.

Example #1

Array1 {1,2,3}
Array2 {1,1,2,2,3,3}

By comparing two arrays, the result should tell that the second array has two copys of the first array.

Example #2

Array1 {1,2,3}
Array2 {1,1,2,2,3,4}

Array2 contains only one copy of Array1 which is:

Array1 {1,2,3} and a portion of
Array2 {1,2,3} match but
Array1 {1,2,3} and the rest of the Array2 {1,2,4} does

Only one copy

Example #3

Array1 {1,2,3}
Array2 {1,2,4}

No copy of Array1 is contained by Array2

I did already have a solution but it is quite complicated. It is like this:

In case there are two arrays

Array1 {1,2,3}
Array2 {1,2,3,4}

First compare the first element of the Array1 with ever elements of the Array2. You will find there is indeed a match for the first element of Array1.

Register the match and remove the matched element found in Array2 then compare the second element of the Array1 with all remaining element of the Array2. If all Array1 element has been registered as match-found then there is a copy, if Array2 still have remaining elements, the start the comparation again the see whether there is any more copys.

I illustrate it with diagram below:

Case #1
Array1 {1,2,3}
Array2 {1,2,3,4}

Compare Array1 first element {1} with Array2. A match is found. Register the match.

Array1 register:
{1 - found}
{2}
{3}

Then remove the match element in Array2, Array2 will be {2,3,4}

Start to compare the second element of Array1 {2} to all remaining elements of Array2.

A match found, register the match.

Array 1 register
{1 - found}
{2 - found}
{3}

Then remove the match element in Array2, Array2 will be {3,4}

Start to compare the third element of Array1 {3} to all remaining elements of Array2.

A match found, register the match.

Array 1 register
{1 - found}
{2 - found}
{3 - found}

Now all elements of Array1 has been compared and a copy found, however, there are still some elements left in Array2, so we need to compare run the comparation one more time.

After comparing all elements of Array1 with the rest elements of Array2, we found no match. So return the copy number, which is 1.
 

Users who are viewing this thread

Back
Top Bottom