Compare two NSArrays and return number of differences

前端 未结 2 1175
情书的邮戳
情书的邮戳 2020-12-31 17:22

How can I take two NSArrays, compare them, then return the number of differences, preferably the number of different objects, for example:

Array 1: one two

2条回答
  •  借酒劲吻你
    2020-12-31 18:03

    You can do this by using an intermediate NSMutableArray:

    NSArray *array1 = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];
    NSArray *array2 = [NSArray arrayWithObjects:@"Two", @"Four", @"One", nil];
    NSMutableArray *intermediate = [NSMutableArray arrayWithArray:array1];
    [intermediate removeObjectsInArray:array2];
    NSUInteger difference = [intermediate count];
    

    With that way, only common elements will be removed.

提交回复
热议问题