ios方法实现查找两个数组中相同数o(n)

2018-06-29  本文已影响0人  大写的空气

NSArray *array1 = @[@7,@1,@77,@2,@0,@9,@56];

    NSArray *array2 = @[@37,@21,@177,@7,@1,@5,@0,@19,@52];

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    for(inti =0; i < array1.count; i++){

        [dictsetObject:array1[i]forKey:array1[i]];

    }

    for(inti =0; i < array2.count; i++) {

        if([dictobjectForKey:array2[i]]) {

            NSLog(@"=====字典形式实现,两个数组中相同数为:%@",[dict objectForKey:array2[i]]);

        }

    }

    for(inti =0; i < array2.count; i++) {

        if([array1containsObject:array2[i]]) {

            NSLog(@"=====数组形式实现,两个数组中相同数为:%@",[dict objectForKey:array2[i]]);

        }

    }

2018-06-29 18:37:06.905194+0800 Cling_ios[96801:10864048] =====字典形式实现,两个数组中相同数为:7

2018-06-29 18:37:06.905591+0800 Cling_ios[96801:10864048] =====字典形式实现,两个数组中相同数为:1

2018-06-29 18:37:06.906007+0800 Cling_ios[96801:10864048] =====字典形式实现,两个数组中相同数为:0

2018-06-29 18:37:06.906364+0800 Cling_ios[96801:10864048] =====数组形式实现,两个数组中相同数为:7

2018-06-29 18:37:06.906653+0800 Cling_ios[96801:10864048] =====数组形式实现,两个数组中相同数为:1

2018-06-29 18:37:06.907790+0800 Cling_ios[96801:10864048] =====数组形式实现,两个数组中相同数为:0

上一篇 下一篇

猜你喜欢

热点阅读