美文网首页
ios方法实现查找两个数组中相同数o(n)

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

作者: 大写的空气 | 来源:发表于2018-06-29 18:41 被阅读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

相关文章

网友评论

      本文标题:ios方法实现查找两个数组中相同数o(n)

      本文链接:https://www.haomeiwen.com/subject/gwtzyftx.html