Symmetric Difference of Two Arrays
I have two NSArray
s of Movie objects called DVD and VHS. I'd like to find the symmetric diff开发者_如何学Cerence 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.
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
.
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.
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.
精彩评论