0

In my project I have two NSArray and both array contains two values frequency and key. Now I have to compare these two NSArray with the reference of key, then I have to find common key and from this common key I have to store frequencies of each array in another array so that I have common count for each array

Example

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 Above array data in 1st array I have 6 count and in another I have 5 count Please help me to find common key from these two NSArray

Nishi
  • 683
  • 1
  • 12
  • 31

1 Answers1

4

Used Set this is simplest way to find common values.

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];

Check Swift code:-

var set1 = Set<AnyHashable>(yourFirstArray)
var set2 = Set<AnyHashable>(yourSecondArray)
set1.intersect(set2) //this will give you only the obejcts that are in both sets

let result = Array(set1)
Jitendra
  • 5,055
  • 2
  • 22
  • 42