Finding Intersection of NSMutableArrays

前端 未结 5 1915
广开言路
广开言路 2020-11-28 09:30

I have three NSMutableArray containing names that are added to the lists according to different criterieas.

Here are my arrays pseudocode:

NSMutableA         


        
5条回答
  •  心在旅途
    2020-11-28 10:27

    Use NSMutableSet:

    NSMutableSet *intersection = [NSMutableSet setWithArray:array1];
    [intersection intersectSet:[NSSet setWithArray:array2]];
    [intersection intersectSet:[NSSet setWithArray:array3]];
    
    NSArray *array4 = [intersection allObjects];
    

    The only issue with this is that you lose ordering of elements, but I think (in this case) that that's OK.


    As has been pointed out in the comments (thanks, Q80!), iOS 5 and OS X 10.7 added a new class called NSOrderedSet (with a Mutable subclass) that allows you to perform these same intersection operations while still maintaining order.

提交回复
热议问题