ios - delete an element in the array

  • To delete an element in the array
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"1",@"2",@"3",@"1",@"5", nil];
    for (int i=0; i<array.count; i++) {
        NSString *str = array[i];
        if ([str isEqualToString:@"1"]) {
            [array removeObjectAtIndex:i];
            i--;
        }
    }
    NSLog(@"%@",array);
  • Deduplication Array
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"1",@"1",@"1",@"2",@"2",@"3",@"1",@"2",@"2",@"5",@"8888", nil];
    for (int i=0; i<array.count; i++) {
        NSString *str1 = array[i];
        for (int j=i+1; j<array.count; j++) {
            NSString *str2 = array[j];
            if ([str2 isEqualToString:str1]) {
                [array removeObjectAtIndex:j];
                j--;
            }
        }
    }
    NSLog(@"%@",array);

Reproduced in: https: //www.jianshu.com/p/612a7111ee4e

Guess you like

Origin blog.csdn.net/weixin_33681778/article/details/91075689