- 直接上代码
for循环数组去重
NSArray * arr = @[@1,@2,@3,@4,@1,@3,@4,@0,@6,@1,@5];
NSMutableArray *arrM = [NSMutableArray arrayWithArray:arr];
for (int i = (int)arrM.count-1; i >= 0; i --) {
id a = arrM[i];
for (int j = (int)arrM.count-1; j >= 0; j --) {
id b = arrM[j];
if (a == b && i != j) {
[arrM removeObjectAtIndex:i];
break;
}
}
}
- 打印结果
arr:(
1,
2,
3,
4,
0,
6,
5
)
续 07-03
数组升序降序
NSArray *arr = @[@1,@4,@7,@3,@9,@6];
NSArray *resut = [arr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];//升序,obj1和obj2调换顺序则为降序;
}];
DLog(@"resut:%@",resut);
- 打印结果
resut:(
1,
3,
4,
6,
7,
9
)