如何从两个NSArray中找到共同的价值?

时间:2013-10-15 06:41:46

标签: ios count nsarray

在我的项目中,我有两个NSArray,两个数组都包含两个值frequency和key。 现在我必须将这两个NSArray与密钥的引用进行比较,然后我必须找到公共密钥,并且从这个公共密钥我必须将每个数组的频率存储在另一个数组中,以便我对每个数组都有共同的计数

实施例

Printing description of xSeriesArray:

{
  frequency = 60;
  key = 5591090;
},
{
  frequency = 50;
  key = 5591089;
},
{
  frequency = 40;
  key = 5591082;
},
{
 frequency = 30;
 key = 5591078;
},
{
 frequency = 20;
 key = 5591077;
},
{
 frequency = 10;
 key = 5591076;
}


Printing description of ySeriesArray:
<__NSArrayM 0xa1e1270>
  {
   frequency = 500;
   key = 5591089;
  },
  {
   frequency = 400;
   key = 5591082;
 },
 {
  frequency = 300;
  key = 5591078;
  },
 {
  frequency = 200;
  key = 5591077;
 },
 {
  frequency = 100;
  key = 5591076;
 }

On在第一个阵列中的数组数据上我有6个计数而在另一个数组中我有5个计数 请帮我找到这两个NSArray的常用密钥

1 个答案:

答案 0 :(得分:3)

使用Set这是查找常用值的最简单方法。

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

NSArray* result = [set1 allObjects];