2

pseudo code:

NSArray *arr1 = {"mike","george","Tim","Tom"...}
NSArray *arr2 = {"mike","george","Chris","Adam"...}
NSArray *arr3 = {"Don","george","mike","Tom"...}
NSArray *arr4 = {"george","Nicole","Moe","mike"...}

//Add these arrays to the dictionary
for(int i=0; i<n, i++)
   [dictionary addObject:arr[i]];

I would like to find the duplicate value(s) that exists in ALL of the arrays, not some. In this example, the value would be {"george","mike"}.

4

2 に答える 2

3
NSArray *arr1 = @[@"mike",@"george",@"Tim",@"Tom" ];
NSArray *arr2 = @[@"mike",@"george",@"Chris",@"Adam" ];
NSArray *arr3 = @[@"Don",@"george",@"mike",@"Tom" ];
NSArray *arr4 = @[@"george",@"Nicole",@"Moe",@"mike" ];

NSMutableSet *intersection = [NSMutableSet setWithArray:arr1];
[intersection intersectSet:[NSSet setWithArray:arr2]];
[intersection intersectSet:[NSSet setWithArray:arr3]];
[intersection intersectSet:[NSSet setWithArray:arr4]];

NSArray *duplicates = [intersection allObjects];

NSLog(@"duplicates=%@", duplicates);

duplicates には、それらすべてに表示される値を持つ配列が含まれています。

duplicates=(
  mike,
  george
)
于 2012-11-22T00:09:33.003 に答える
-1

dictionaryが実際に NSMutableArray であると仮定すると、次のようなものが機能します。

NSMutableArray *results = [[NSMutableArray alloc] init];

for(NSString *name in [dictionary objectAtIndex:0]) {
    bool valid = true;
    for(int i=1;i<[dictionary count];i++) {
        if([[dictionary objectAtIndex:i] indexOfObject:name]==NSNotFound) {
            valid=false;
            break;
        }
    }
    if(valid)
        [results addObject:name];
}
于 2012-11-22T00:07:47.207 に答える