比较两个NSArrays和返回的差异数 [英] Compare two NSArrays and return number of differences

查看:126
本文介绍了比较两个NSArrays和返回的差异数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取两个NSArrays,比较它们,然后返回差异数,最好是不同对象的数量,例如:

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

$ b one
two
three

Array 1: one two three

数组2:
two
four
one

Array 2: two four one

我想要返回1

推荐答案

使用中间 NSMutableArray a>:

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.

这篇关于比较两个NSArrays和返回的差异数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆