views:

211

answers:

3

I have two NSArrays of Movie objects called DVD and VHS. I'd like to find the symmetric difference of these arrays. I want to know which Movies are in VHS buy not in DVD, and which Movies are in DVD but not VHS.

Can anyone tell me if there is a fast algorithm to solve it (preferably in C or Objective-C)? Is it faster/easier to solve if I use Dictionaries? What this kind of problem called (or is it just "Symmetric Difference")?

Thanks.

A: 

If you need VHS minus DVD, and DVD minus VHS in two different arrays, use -removeObjectsInArray:.

If you need them both in the same array, sort them and try to re-implement this algorithm in ObjC.

KennyTM
A: 

Try sorting both of the arrays using Title of the Movie (MergeSort), then merge both of them and modify the merge function so that it prints unique uncommon elements.

bhups
This sounds close to what I was thinking of. Sort the arrays (I have ids) then step through both arrays at the same time, comparing and finding matches or holes / uncommon elements. Do you know where I can find code for this? Is this called a Merge Sort?
nevan
Yes, it is called NergeSort. You can find lots of example on the net. here is one: http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Merge_sort
bhups
A: 

You might get better results using NSSet rather than NSArray, depending on whether or not you want to allow duplicates in your lists.

NSSet gives you methods like intersectsSet: which should give you what you need.

If you need union functionality, you can use NSMutableSet.

Jasarien
Symmetric difference is `(A union B) minus (A intersect B)`, or equivalently, `(A minus B) union (B minus A)`.
KennyTM
There are no duplicates in my arrays. `intersectsSet:` seems to only return a `BOOL` telling you whether or not they do intersect. I'm looking for the opposite of an intersection. I looked through the `NSSet` docs, but couldn't see a method like the one I need.
nevan