1, the elements in the array to repeat
For example:
| 1 |
NSArray *array = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"]; |
Reference Answer:
The first method: open up new memory space, and then determine if there is, if not exist, add to the array, the order of the final result is not changed. Efficiency analysis: Time complexity of O (N2):
| 12345678910 |
NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:array.count];// 外层一个循环for(NSString *item inarray) { // 调用-containsObject:本质也是要循环去判断,因此本质上是双层遍历 // 时间复杂度为O ( n^2 )而不是O (n) if(![resultArray containsObject:item]) { [resultArray addObject:item]; }}NSLog(@"resultArray: %@", resultArray); |
Add: The original collection operation can be implemented by Valueforkeypath, and a single line of code can be implemented:
| 12 |
array = [array valueForKeyPath:@"@distinctUnionOfObjects.self"];NSLog(@"%@", array); |
However, the returned results are unordered and differ from the original order. You can read: Collection Operators
The second method: use Nsdictionary to go to the weight, the dictionary when set Key-value, if already exist update value, if not exist insert value, then get allvalues. This method can be used if the order is not required. If ordered, it has to be sorted. Efficiency analysis: Only need a loop can be completed into the dictionary, if not ordered, the time complexity of O (n). If ordering is required, efficiency is related to the sorting algorithm:
| 123456 |
NSMutableDictionary *resultDict = [[NSMutableDictionary alloc] initWithCapacity:array.count];for(NSString *item inarray) { [resultDict setObject:item forKey:item];}NSArray *resultArray = resultDict.allValues;NSLog(@"%@", resultArray); |
If you need to sort in the original ascending order, you can:
| 123456 |
< Code class= "JS Plain" >resultarray = [resultarray sortedarrayusingcomparator:^nscomparisonresult ( ID&NBSP;&NBSP;_NONNULL&NBSP;OBJ1,&NBSP;ID&NBSP;&NBSP;_NONNULL&NBSP;OBJ2) { NSSTRING&NBSP;*ITEM1&NBSP;=&NBSP;OBJ1; &NBSP;&NBSP; NSSTRING&NBSP;*ITEM2&NBSP;=&NBSP;OBJ2; &NBSP;&NBSP; return [item1 compare:item2 options: Nsliteralsearch]; nslog (@ "%@" , resultarray); |
The third method: using the Set nsset characteristics (certainty, disorder, cross-specific), put into the set will automatically go heavy. But it has the same disorder as the dictionary, and the resulting order is no longer the same as the original. If order is not required, the efficiency of using this method should be similar to that of a dictionary. Efficiency analysis: Time complexity of O (n):
| 123 |
NSSet *set = [NSSet setWithArray:array];NSArray *resultArray = [set allObjects];NSLog(@"%@", resultArray); |
If ordered, it would have to be sorted, for example, in ascending order:
| 123456 |
resultArray = [resultArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { NSString *item1 = obj1; NSString *item2 = obj2; return[item1 compare:item2 options:NSLiteralSearch];}];NSLog(@"%@", resultArray); |
Fourth method
| 12 |
NSOrderedSet *set = [NSOrderedSet orderedSetWithArray:array];NSLog(@"%@", set.array); |
Array de-weight