找出数组中重复的元素
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);
网友评论