nevan king
nevan king

Reputation: 113777

Symmetric Difference of Two Arrays

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.

Upvotes: 3

Views: 2789

Answers (3)

Jasarien
Jasarien

Reputation: 58468

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.

Upvotes: 2

bhups
bhups

Reputation: 14895

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.

Upvotes: 0

kennytm
kennytm

Reputation: 523664

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.

Upvotes: 1

Related Questions