Compare two NSArrays and return number of differences

前端 未结 2 1174
情书的邮戳
情书的邮戳 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 17:55

    I found that the answer above didn't take arrays of differing size into account. If you do it as above, you should check to see which array.count is smaller and

    [largerArray removeObjectsInArray:shorterArray];
    

    OR

    I made them both NSSets and then compared.

    [set1 isEqualToSet:set2];
    

    That way size and order are both dealt with properly! (I didn't need to know the number of differences)

提交回复
热议问题