Re: Comparing arrays
Re: Comparing arrays
- Subject: Re: Comparing arrays
- From: "Daniele Corti" <email@hidden>
- Date: Mon, 11 Jun 2007 09:39:46 +0200
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.
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
thanks
mich
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
hope this help!
--
Daniele Corti
AIM: S0CR4TE5
Messenger:
email@hidden
_______________________________________________
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