473,890 Members | 1,513 Online

# comparing arrays

If I have two arrays, what is a good paradigm for comparing what is in
them, to determine what elements they share, or don't share?

Specifically, each array could potentially contain the integers 1 to
9, but probably not all 9 integers.

MyArray1 = 1,2,3,5,6,7,8
MyArray2 = 1,4,6,7

I would need the answers to be arrays too:
MyArrayShared: 1,6,7
MyArrayNotShare d: 9

TIA
Jan 18 '06 #1
1 2249
On Wed, 18 Jan 2006 10:16:32 GMT, Donald Grove
<do*********@ve rizon.net> wrote:

You could go brute-force since these are such small arrays. Just
iterate over the one, and for each element iterate over the second.

If you needed maximum speed then your algorithm should use all
additional information that is available, for example:
* the arrays are sorted
* the length of an array can be calculated very quickly
* MyArray1 is longer than MyArray2.
There may be other operational characteristics that you can leverage.

-Tom.

If I have two arrays, what is a good paradigm for comparing what is in
them, to determine what elements they share, or don't share?

Specifically , each array could potentially contain the integers 1 to
9, but probably not all 9 integers.

MyArray1 = 1,2,3,5,6,7,8
MyArray2 = 1,4,6,7

I would need the answers to be arrays too:
MyArrayShare d: 1,6,7
MyArrayNotShar ed: 9

TIA

Jan 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 11 461 by: Peter | last post by: Hi how can I compare two byte arrays in VB.NET Thank Peter 1 495 by: Iain | last post by: Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and checking to see if the result has any 1s in it (if so, the arrays are different). This seems to be faster than comparing each bit of the two original arrays one at a time. But I still have to iterate over each element of the array, and I'd like to 12 885 by: Elijah Bailey | last post by: I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my application. I tried bcmp / a loop for comparison, but it seems they are very slow compared to comparing longs...Any ideas? I tried splitting the array into longs and comparing, but then I face the high endian/low endian problem on some machines. 4 10547 by: agent349 | last post by: First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three sets of character strings from the user. Then I want to run through each element and compare the two strings. If they match I print they match... I'm having a bit of trouble with the actual loop through each array using the pointers and comparing... 4 6027 by: eoghan.kenny | last post by: Hi, I need to compare two timestamp columns in sql server and see which one is greater. (i can tell if they are not equal buts not enough for this requirement). A timestamp value is unique in a database and always increments. A non-null timestamp is type Binary(8) in sql server and I pass it to a byte array in C#. To compare the two byte arrays I use BitConverter.ToUInt64, as this converts an 8 byte array to a 64 bit 19 3860 by: Ole Nielsby | last post by: How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that handles GetHashCode and Equal? If so, is such a wrapper present in the standard class library? 11 10390 by: Sheldon | last post by: Hi, I have two arrays that are identical and contain 1s and zeros. Only the ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work but this example proves otherwise: >>> a = >>> b = >>> Numeric.logical_and(a==6,b==6) 0 1 7704 by: psmahesh | last post by: Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there is a better way to achieve it for(i=0;i