Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 542
dfsf Avatar asked Mar 22 '26 08:03

dfsf


1 Answers

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];
like image 154
albertamg Avatar answered Mar 24 '26 20:03

albertamg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!