如何从多个数组中提取唯一对象

时间:2012-06-05 05:25:22

标签: objective-c ios5 nsarray

修改
我有两个不同的数组和一些重复的字符串,我想创建一个只有唯一字符串的新数组。

例如,拿这两个数组:

NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil]; 
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];

// Result should be an array with objects "b", and "d" 
// since they are the only two that are not repeated in the other array.

4 个答案:

答案 0 :(得分:6)

编辑:

// Your starting arrays
NSArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil]; 
NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];

// Create two new arrays that only contain the objects 
// which are not in the other array:
NSMutableArray *uniqueElementsInArray1 = [array1 mutableCopy];
[uniqueElementsInArray1 removeObjectsInArray:array2];

NSMutableArray *uniqueElementsInArray2 = [array2 mutableCopy];
[uniqueElementsInArray2 removeObjectsInArray:array1];

// Combine the two arrays.
// Result contains objects @"b" and @"d":
NSArray *result = [uniqueElementsInArray1 arrayByAddingObjectsFromArray:uniqueElementsInArray2];

答案 1 :(得分:1)

为此,您只需声明另一个临时NSMutableArray。从原始数组中检索您拥有的任何数据,例如objectArray。检查临时数组是否具有该值,并将其放入临时数组中。请参考以下代码:

    for(NSString *str in objectArray)
    {

        if(![tempArray containsObject:str])
        {

            [tempArray addObject:str];

        }
    }

在此之后,如果你想进一步使用objectArray,你可以继续使用tempArray或将tempArray放入objectArray。我认为这应该适合你。

答案 2 :(得分:1)

您可以使用NSSet作为过滤器(想想脑海中的维恩图):

<div style="height:120px;width:120px;overflow-x:scroll;overflow-y:scroll;">
As you can see, once there's enough text in this box, the box will grow scroll bars... that's why we call it a scroll box! You could also place an image into the scroll box.
</div>
方法1(我最喜欢的):
NSArray *array1 = @[@1,@2,@3,@4,@2,@3];
NSArray *array2 = @[@3,@4,@5,@6,@4,@6];

NSSet *set1 = [NSSet setWithArray:array1]; // [1,2,3,4]
NSSet *set2 = [NSSet setWithArray:array2]; // [3,4,5,6]
方法2(更明确的测试,不需要维恩图):
NSMutableSet *mSet1 = [set1 mutableCopy];
NSMutableSet *mSet2 = [set2 mutableCopy];

[mSet1 minusSet:set2]; // mSet1 = [1,2]
[mSet2 minusSet:set1]; // mSet2 =  [5,6]

[mSet1 unionSet:mSet2]; // mSet1 = [1,2,5,6], only the unique elements.

// Now just put it in an immutable collections with a self-docu name...
NSArray *arrayOfUniqueness = [setOfUniqueElementsOnly allObjects];
方法3(避免NSSet *setOfObjsUniqueTo1 = [set1 objectsPassingTest:^BOOL(id _Nonnull obj, BOOL * _Nonnull stop) { return ![set2 containsObject:obj]; }]; // [1,2] NSSet *setOfObjsUniqueTo2 = [set2 objectsPassingTest:^BOOL(id _Nonnull obj, BOOL * _Nonnull stop) { return ![set1 containsObject:obj]; }]; // [5,6] NSMutableSet *oneSetToRuleThemAll = [NSMutableSet setWithSet:setOfObjsUniqueTo1]; // [1,2] [oneSetToRuleThemAll unionSet:setOfObjsUniqueTo2]; // [1,2,5,6] // Or as an array: NSArray *anotherArrayOfUniqueness = [oneSetToRuleThemAll allObjects]; ,但我不会在正式晚宴上将这段代码放在英格兰女王对面 - 这是不优雅的):
NSSet

不是提问者的问题,但为了获得一个删除重复项的数组(即,每个元素唯一),可以做类似的魔术:

NSMutableArray *mArray1 = [NSMutableArray new];
NSMutableArray *mArray2 = [NSMutableArray new];

NSIndexSet *uniqueIndexes1 = [array1 indexesOfObjectsPassingTest:^BOOL(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    return ![array2 containsObject:obj];
}]; // [0,1,4] (b/c @1 and @2 are unique to array1)

[uniqueIndexes1 enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
    [mArray1 addObject:array1[idx]];
}]; // @[@1,@2,@2]

NSIndexSet *uniqueIndexes2 = [array2 indexesOfObjectsPassingTest:^BOOL(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    return ![array1 containsObject:obj];
}]; // [2,3,5] (b/c @5 and @6 are unique to array2)

[uniqueIndexes2 enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
    [mArray2 addObject:array2[idx]];
}]; // @[@5,@6,@6]

NSArray *unionArray = [array1 arrayByAddingObjectsFromArray:array2];
    // @[@1,@2,@2,@5,@6,@6]

NSArray *yetAnotherArrayOfUniqueness = [[NSSet setWithArray:unionArray] allObjects];
    // @[@1,@2,@5,@6]

根据Apple Docs(强调添加):

  

+ setWithArray:   创建并返回一个包含给定数组中包含的对象的 uniqued 集合的集合。

更新:请点击此处查看类似问题:Remove all strings with duplicates in an NSArray

答案 3 :(得分:-1)

使用Set作为过滤器,例如:

String[] arr = {"a","a","b"};
Object[] uniqueArr = (Object[])new HashSet<String>(Arrays.asList(arr)).toArray();