## Interview Question

IOS Developer Interview

-Singapore

# 1. Given a 3 array like below NSArray *a = [1,3,4,5]; NSArray *b = [-1,3,0,9]; NSArray *c = [0,31,32,22,6]; Find the elements from the three array which existing in atleast 2 arrays. Eg: [3, 0] Because 3 is presented in array a, b and 0 is presented in array b, c respectively. -(NSArray)find2ElementsAtleastPresentIn2Arrays:(NSArray*)aList b::(NSArray*)bList c::(NSArray*)cList{ // -- your code here. }

8

- (NSArray*)find2ElementsAtleastPresentIn2Arrays:(NSArray*)aList b:(NSArray*)bList c:(NSArray*)cList { NSMutableArray *bigArray = [NSMutableArray array]; [bigArray addObjectsFromArray:aList]; [bigArray addObjectsFromArray:bList]; [bigArray addObjectsFromArray:cList]; NSMutableArray *resultArray = [NSMutableArray array]; NSCountedSet *cs = [[NSCountedSet alloc] initWithArray:bigArray]; for(NSNumber *num in cs){ if([cs countForObject:num]>1) if (![resultArray containsObject:num]) { [resultArray addObject:num]; } } return resultArray; }

Jatin Dhawan on

1

NSArray *a = [[NSArray alloc] initWithObjects:@(1),@(3),@(4),@(5),@(5), nil]; NSArray *b = [[NSArray alloc] initWithObjects:@(-1),@(3),@(0),@(9),@(4), nil]; NSArray *c = [[NSArray alloc] initWithObjects:@(0),@(31),@(32),@(22),@(6), nil]; NSOrderedSet *orderedSetA = [NSOrderedSet orderedSetWithArray:a]; NSOrderedSet *orderedSetB = [NSOrderedSet orderedSetWithArray:b]; NSOrderedSet *orderedSetC = [NSOrderedSet orderedSetWithArray:c]; NSMutableArray *collectionArray = [[NSMutableArray alloc] init]; NSMutableArray *resultArray = [[NSMutableArray alloc] init]; [collectionArray addObjectsFromArray:[orderedSetA array]]; [collectionArray addObjectsFromArray:[orderedSetB array]]; [collectionArray addObjectsFromArray:[orderedSetC array]]; NSCountedSet *cs = [[NSCountedSet alloc] initWithArray:collectionArray]; for (NSNumber *number in cs) { if ([cs countForObject:number] > 1) { if (![resultArray containsObject:number]) { [resultArray addObject:number]; } } } NSLog(@"%@",resultArray);

Shamsudheen TK on

2

Anonymous on

0

+ (NSArray*) findCommonElements:(NSArray*)a and:(NSArray*)b and:(NSArray*)c { NSMutableArray * common = [NSMutableArray new]; NSMutableArray * combined = [NSMutableArray new]; [combined addObjectsFromArray:a]; [combined addObjectsFromArray:b]; [combined addObjectsFromArray:c]; NSCountedSet * set = [[NSCountedSet alloc]initWithArray:combined]; for (id obj in set) { if ([set countForObject:obj] >= 2) { [common addObject:obj]; } } return common; }

Anonymous on

0

union(intersection (set(array1), set(array2)) , intersection (set(array2), set(array3)) , intersection (set(array1), set(array3)))

Oleh Kudinov on

0

Put elements all three array in 3 sets, then put all the elements from these sets to a counted set (NSCountedSet), now just use a predicate to fined which all elements are there in this final set with count more than 2.