IOS如何比较具有不同值和元素数量的两个数组

时间:2022-12-16 12:06:42

I have two array, array A has 4 elements and array B has 10 elements. How to compare this two array together to find out whether array A has values that contains in array B.

我有两个数组,数组A有4个元素,数组B有10个元素。如何将这两个数组进行比较,以确定数组A是否包含数组B中包含的值

Here is the codes.

这是代码。

   for(int i = 0; i <= deepsightSig.count; i++){
    for(int p = 0; p <= feeds.count; i++){


        if(feeds[i] == deepsightSig[i]){

            badIPCount++;


        }
        else
            goodIPCount++;

    }


}

1 个解决方案

#1


8  

NSMutableSet* set1 = [NSMutableSet setWithArray:array1];
NSMutableSet* set2 = [NSMutableSet setWithArray:array2];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets

NSArray* result = [set1 allObjects];

if result.count is greater than one it means array A have values that are in array B.

如果result.count大于1,则表示数组A具有数组B中的值。

#1


8  

NSMutableSet* set1 = [NSMutableSet setWithArray:array1];
NSMutableSet* set2 = [NSMutableSet setWithArray:array2];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets

NSArray* result = [set1 allObjects];

if result.count is greater than one it means array A have values that are in array B.

如果result.count大于1,则表示数组A具有数组B中的值。