1

First_mutableArray1,2,3,4,5,6
Second_MutableArray2,4,6,8,0,12

如何获得这样的输出

First_mutableArray1,2,3,4,5,6,8,0,12

4

7 回答 7

9

订购版本:

    NSMutableOrderedSet *first = [NSMutableOrderedSet orderedSetWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil];
    NSOrderedSet *second = [NSOrderedSet orderedSetWithObjects:@"2",@"4",@"6",@"8",@"0",@"12",nil];
    [first unionOrderedSet:second];

变量first将包含结果。

于 2013-03-07T13:02:23.920 回答
2

尝试 :

NSMutableArray *first=[NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5",@"6"]];
NSMutableArray *second=[NSMutableArray arrayWithArray:@[@"2",@"4",@"6",@"8",@"0",@"12"]];

for (id obj in second) {
    if (![first containsObject:obj]) {
        [first addObject:obj];
    }
}
NSLog(@"%@",first);

编辑:

NSMutableArray *first=[NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5",@"6"]];
NSMutableArray *second=[NSMutableArray arrayWithArray:@[@"2",@"4",@"6",@"8",@"0",@"12"]];


NSMutableOrderedSet *firstSet=[NSMutableOrderedSet orderedSetWithArray:first];
NSOrderedSet *secondSet=[NSOrderedSet orderedSetWithArray:second];

[firstSet unionOrderedSet:secondSet];
first=[[firstSet array] mutableCopy];

NSLog(@"%@",first);

*归功于 Mark Kryzhanouski

于 2013-03-07T12:51:31.953 回答
0

组合这两个数组并在组合数组上使用下面的代码两个删除重复项。

NSMutableArray* combinedArray = [NSMutableArray arrayWithArray:firstArray];
[combinedArray addObjectsFromArray: secondArray];
NSMutableArray *myUniqueArray = [NSMutableArray array];

for (id obj in combinedArray) {
    if (![myUniqueArray containsObject:obj]) {
        [myUniqueArray addObject:obj];
    }
}
于 2013-03-07T12:52:23.013 回答
0

使用此代码

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];
     NSMutableArray *array1 = [[NSMutableArray alloc]initWithObjects:@"2",@"4",@"6",@"8",@"0",@"12", nil];
    for(int i = 0;i<array1.count;i++)
    {
        NSString *str = [array1 objectAtIndex:i];
        if (![array containsObject:str])
        {
            [array addObject: str]; 
        }else
        {
            NSLog(@"contins");
        }
    }
    NSLog(@"%@",array);
于 2013-03-07T12:52:25.283 回答
0

你可以使用NSMutableSet来做这些事情:

NSMutableSet *firstSet  = [NSMutableSet setWithArray:First_mutableArray];
NSMutableSet *secondSet = [NSMutableSet setWithArray:Second_MutableArray];
[firstSet unionSet:secondSet];
NSArray *uniqueArray    = [firstSet allObjects];
于 2013-03-07T13:00:48.467 回答
0

给你。与遍历每个数组并添加不匹配的对象相比,使用 NSSet 将为您提供更好的性能。

NSArray *arr1 = @[@1,@2,@3,@4,@5,@6];
NSArray *arr2 = @[@2,@4,@6,@8,@0,@12];

NSMutableSet *set = [[NSMutableSet alloc] initWithArray:arr1];
[set addObjectsFromArray:arr2];

// Unsorted
NSLog(@"set = %@", [set description]);

// Sorted
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
NSArray *sortedArray = [set sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"sortedArray = %@", [sortedArray description]);
于 2013-03-07T13:05:32.340 回答
0

试试下面的一个:

NSMutableArray *first=[NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5",@"6"]];
NSMutableArray *second=[NSMutableArray arrayWithArray:@[@"2",@"4",@"6",@"8",@"0",@"12"]];

NSMutableSet *firstSet  = [NSMutableSet setWithArray:first];
NSMutableSet *secondSet = [NSMutableSet setWithArray:second];
[firstSet unionSet:secondSet];
NSArray *uniqueArray    = [firstSet allObjects];
于 2013-03-07T13:06:54.617 回答