开发者

NSArray most effecient way to do this:

Let's say I have an array (NSArray) called arrayA -> {@"A", @"B", @"C", @"D开发者_开发问答", @"E"}. And I have another array(NSArray) called arrayB -> {@"D", @"E", @"F", @"G", @"H"}.

What's the most efficient way for me to get two arrays: 1. An array that is subset of both of them, so in this case it will be {@"D", @"E"} and a subset that is unique in arrayB, so {@"F", @"G", @"H"}.

It's easier if you look at this ven diagram: http://theconsigliori.com/blog/wp-content/uploads/2009/09/venn-diagram.jpg

A and B are arrays, I want to get 2 arrays, 1. A&B 2. B-(A&B).

I am using objective-c / cocoa-touch, but any general idea is welcome. The array are going to be approximately 6000 elements long and I am doing this on an iPad.

Thanks!


Intersection of the two arrays:

NSMutableSet *intersectionDict = [NSMutableSet setWithArray:arrayA];
[intersectionDict intersectSet:[NSSet setWithArray:arrayB]];
NSArray *intersectionArray = [intersectionDict allObjects];

Subset of objects in arrayB that are not present in arrayA:

NSMutableArray *arrayC = [NSMutableArray arrayWithArray:arrayB];
[arrayC removeObjectsInArray:arrayA];


The method intersectSet, as stated in this post:

NSArray - check if objects are in an array? solves your problem in an Apple-efficiency way. Should be enough?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜