美文网首页
iOS-Code Snippet

iOS-Code Snippet

作者: 和谐共处 | 来源:发表于2019-03-26 12:58 被阅读0次

找出数组中重复的元素

    
    NSMutableArray *arr = @[@(1),@(2),@(2),@(3),@(4),@(5),@(6),@(7),@(1),@(3)].mutableCopy;
    NSMutableArray *repeats = @[].mutableCopy;
    for (int i = 0; i<arr.count; i++) {
        for (int j = i+1; j<arr.count; j++) {
            NSNumber *num = arr[i];
            NSNumber *nextNum = arr[j];
          
            if (num.intValue == nextNum.intValue) {
                NSDictionary *repeat = @{@"preIndex":@(i),
                                         @"nextIndex":@(j),
                                         @"value":num,
                                         };
                [repeats addObject:repeat];
            }
        }
    }
    NSLog(@"repeats = %@",repeats);

相关文章

网友评论

      本文标题:iOS-Code Snippet

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