iOS-数组集合操作(NSMutableSet)

NSMutableArray *arr1 = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];

    

NSMutableArray *arr2 = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];

    

NSMutableSet *set1 = [NSMutableSet setWithArray:arr1];

    

NSMutableSet *set2 = [NSMutableSet setWithArray:arr2];


// 取差集

[set1 minusSet:set2];

NSLog(@"差集:%@",set1.allObjects); // 差集的结果为:4,5

   

// 取并集

[set1 unionSet:set2];

 NSLog(@"并集:%@",set1.allObjects); // 并集的结果为:1,2,3,4,5

    

// 取交集

[set1 intersectSet:set2];

NSLog(@"交集:%@",set1.allObjects); // 交集的结果为:1,2,3


你可能感兴趣的:(iOS开发)