去掉数组中相同的元素

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/st646889325/article/details/80109253

NSArray*  array = @[@"9",@"1",@"5",@"6",@"5",

                        @"2",@"8",@"7",@"3",@"0",

                        @"5",@"6",@"4",@"8"];

    

    NSMutableArray *temp1 = [[NSMutableArray alloc] init];

    NSMutableArray *temp2 = [NSMutableArray array];

    for (NSString *str in array)

    {

        if (![temp1 containsObject:str])

        {

            [temp1 addObject:str];  // 取出重复

        }else{

            if (![temp2 containsObject:str]) {

                [temp2 addObject:str]; //哪些重复的值

            }

        }

    }

    __block NSMutableArray *difObject = [NSMutableArray arrayWithCapacity:5];

    //找到temp1中有,temp2中没有的数据

    [temp1 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSNumber *number1 = obj;//[obj objectAtIndex:idx];

        __block BOOL isHave = NO;

        [temp2 enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            if ([number1 isEqual:obj]) {

                isHave = YES;

                *stop = YES;

            }

        }];

        if (!isHave) {

            [difObject addObject:obj];

        }

    }];

    NSLog(@"difObject is%@",difObject);



temp1【9,1,5,6,2,8,7,3,0,4】

temp2【5,6,8】

最后difObject[9,1,2,7,3,0,4]




猜你喜欢

转载自blog.csdn.net/st646889325/article/details/80109253
今日推荐