2013-03-07 99 views
1

First_mutableArray1,2,3,4,5,6
Second_MutableArray2,4,6,8,0,12×2陣列在一個第一陣列retrive兩個值

如何獲得這樣

First_mutableArray輸出1,2,3,4,5,6,8,0,12

+0

您可在此找到答案:http://stackoverflow.com/questions/5815695/merge-two-arrays-while-preserving-the-original -array-order – Greg 2013-03-07 12:48:01

+0

是否有可能在任何數組中有重複值?你在'合併'數組中需要唯一的值嗎?訂單重要嗎? – Alladinian 2013-03-07 12:52:39

回答

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]; 

Variab第一個第一個將包含結果。

+0

+1超級解決方案 – 2013-03-07 13:08:01

+1

這是最好的解決方案。 – 2013-03-07 13:34:05

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); 

* 幸得馬克Kryzhanouski

+0

+1非常乾淨優雅 – Anupdas 2013-03-07 13:02:06

+0

效率低於使用NSSet – 2013-03-07 13:06:20

+0

@ElJay:是的,這比Mark的解決方案效率低。 :) – 2013-03-07 13:07:11

0

結合這兩個陣列和combinedArray 2刪除重複使用下面的代碼。

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

for (id obj in combinedArray) { 
    if (![myUniqueArray containsObject:obj]) { 
     [myUniqueArray addObject:obj]; 
    } 
} 
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); 
0

您可以使用NSMutableSet做這個東西:

NSMutableSet *firstSet = [NSMutableSet setWithArray:First_mutableArray]; 
NSMutableSet *secondSet = [NSMutableSet setWithArray:Second_MutableArray]; 
[firstSet unionSet:secondSet]; 
NSArray *uniqueArray = [firstSet allObjects]; 
+0

看起來優雅,但它會給6,4,2。此外,nsset將釋放與數組中的順序。你需要使用NSOrderedSet /或其可變對象 – 2013-03-07 13:05:11

+0

@AnoopVaidya:感謝評論:)這是一個錯字!修正了 – 2013-03-07 13:14:03

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]); 
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];