NSSet和排序方法、数组去重

NSSet 去掉数组中重复的数
NSSet方法

NSArray *arr = @[@11,@33,@44,@42,@22,@11,@44,@11]; NSSet *set = [NSSet setWithArray:arr]; //集合判断一个数组里面是否包含一个对象,速度最快,若重复很多次,应该考虑用集合 //NSOrderedSet [set containsObject:@11]; arr = [set allObjects];

NSDictionary方法
NSArray *arr = @[@11,@33,@44,@42,@22,@11,@44,@11]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [arr enumerateObjectsUsingBlock:^(id_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { dict[obj] = @1; }]; arr = [dict allKeys]; NSLog(@"%@",arr);

数组排序
NSArray *arr2 = @[@11,@33,@44,@42,@22,@11,@44,@11]; NSMutableArray *arr1 = arr2.mutableCopy; for (int i = 0; i < arr1.count; i++) { for (int j = 0; j < arr1.count; j++) { if (arr1[i] > arr1[j]) { [arr1 exchangeObjectAtIndex:i withObjectAtIndex:j]; } } }

冒泡排序
//冒泡排序 for (int i = 0; i < arr1.count; i++) { for (int j = 0; j < arr1.count - 1 - i; j++) { if (arr1[j] > arr1[j + 1]) { [arr1 exchangeObjectAtIndex:j withObjectAtIndex:j + 1]; } } }

【NSSet和排序方法、数组去重】sort方法排序(快排)
[arr1 sortUsingComparator:^NSComparisonResult(id_Nonnull obj1, id_Nonnull obj2) { if (obj1 > obj2) { //降 return NSOrderedDescending; }else if(obj1 < obj2){ //升 return NSOrderedAscending; } else { return NSOrderedSame; } }];

    推荐阅读