two array retrive both value in one first array

旧时模样 提交于 2020-01-22 16:48:25

问题


First_mutableArray is 1,2,3,4,5,6
Second_MutableArray is 2,4,6,8,0,12

How to get output like this

First_mutableArray is 1,2,3,4,5,6,8,0,12 ?


回答1:


Ordered version:

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

Variable first will contain the result.




回答2:


Try :

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

EDIT:

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

*Credit goes to Mark Kryzhanouski




回答3:


combine those two array and use below code on combinedArray two remove duplicates.

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

for (id obj in combinedArray) {
    if (![myUniqueArray containsObject:obj]) {
        [myUniqueArray addObject:obj];
    }
}



回答4:


Use this code

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



回答5:


Try the below one :

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



回答6:


You can use NSMutableSet for doing this stuff:

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



回答7:


Here ya go. Using an NSSet will give you better performance than iterating over each array and adding objects that don't match.

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


来源:https://stackoverflow.com/questions/15271577/two-array-retrive-both-value-in-one-first-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!