First_mutableArray
です1,2,3,4,5,6
Second_MutableArray
_2,4,6,8,0,12
このような出力を取得する方法
First_mutableArray
ですか1,2,3,4,5,6,8,0,12
?
First_mutableArray
です1,2,3,4,5,6
Second_MutableArray
_2,4,6,8,0,12
このような出力を取得する方法
First_mutableArray
ですか1,2,3,4,5,6,8,0,12
?
注文したバージョン:
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];
変数は最初に結果を含みます。
試す :
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);
*クレジットはMarkKryzhanouskiに送られます
どうぞ。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]);
以下を試してください:
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];
このようなことを行うためにNSMutableSetを使用できます。
NSMutableSet *firstSet = [NSMutableSet setWithArray:First_mutableArray];
NSMutableSet *secondSet = [NSMutableSet setWithArray:Second_MutableArray];
[firstSet unionSet:secondSet];
NSArray *uniqueArray = [firstSet allObjects];
これらの 2 つの配列を結合し、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];
}
}
このコードを使用
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);