Re: Comparing arrays
Re: Comparing arrays
- Subject: Re: Comparing arrays
- From: "Jerry W. Walker" <email@hidden>
- Date: Mon, 11 Jun 2007 07:02:21 -0400
Hi, Michelle,
On Jun 11, 2007, at 3:39 AM, Daniele Corti wrote:
2007/6/11, Michelle Parker <email@hidden>: Hi all
I have a slight problem which I am looking for the most efficient
solution:
I am comparing 2 NSArrays containing numbers, with the aim of
removing the numbers in one array from the other array.
The first array is obtained by rawRowsForSQL which returns the
numbers as Longs.
The second array is obtained by valueForKeyPath which returns the
numbers as Integers.
For this you should try to convert each of them in String, then
reconvert into NSArray using
NSPropertyListSerialization.arrayFromString(String), this shuold
create arrays with the same objects.
As Daniel is implying, you'll need to get the arrays to conform in
type to do anything useful with them. However, I think it would be
faster to type cast the second array to Longs rather than convert to
String and back. That is, for converting the values of the second
array, do something like this:
for (int i = 0; i < B.count(); i++) {
B[i] = new Long((long)((Integer)B.objectAtIndex(i)).int());
}
In fact, you might use a loop similar to the above for loop to obtain
the values of B from its original array source while converting
rather than using valueForKeyPath().
NSArray.removeObjectWithArray or NSSet.subtractSet don't work because
the objects are not equal.
The arrays can be very large, ie. 100,000 objects.
What is the best way to handle this?
Look if you don't reach using that, I think the only way is to
check each element controlling the value of every object. I can
give you a solution to execute this in linear time m+n (where m is
the length of the first array, and m the length of the second).
A = {your first Array: length = m}
B = {your second Array: length = n}
i=0, j=0
while i <m AND j <n do
if A[i] = B[j] then
remove(A, i)
i++
Elseif A[i] > B[j]
j++
Else
i++
End if
End
Daniel's method is both fast and accurate but presumes that both of
your NSArrays are sorted. If the original arrays were sorted, then
this is probably the fastest approach to your goal. If they were not
originally sorted, then the cost of the sorts must be added to weigh
whether this is the fastest approach.
I would suggest timing your results if you're worried about the
delays incurred by these operations. Efficiency of Java operations is
seldom intuitive.
If you can guarantee that each array contains only unique values,
another approach you might try, once the arrays conform in type, is
to create NSSets from each NSArray and do a set subtraction of B from
A, then convert the NSSet result back to an NSArray using NSSet's
allObjects() method. If this represents an option for you, I would
again time the operation before accepting it as faster.
Regards,
Jerry
--
__ Jerry W. Walker,
WebObjects Developer/Instructor for High Performance Industrial
Strength Internet Enabled Systems
email@hidden
203 278-4085 office
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden