Interview Question: 1. Given a 3 array like below... |

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. }

Interview Answer

7 Answers


I didn't answered well. Totally dead, wrote tons and tons of code in xcode, but when it comes to simple questions like this and that too in a notepad without Xcode, my brain totally out of station.

But yeah I tried something, I used to combine all three arrays into a mutable array and put an NSOrderedSet to remove the duplicates.

Then run a loop with new array and find the range the range of objects presented in all three arrays.

But they asked about O(n) complexity, time complexity for my solution, which I don't know totally.

I failed purely because I didn't had a Xcode and almost we can't memorise all the syntax and apis that iOS provides. After the interview I opened the xcode and found an abstract api in NSArray, to find common elements between 2 arrays, then I solved this problem within 2 minutes. Here is the answer below.

Tips: Ask the interviewer whether you can use Xcode IDE for testing your codes. I didn't tried it.

- (NSArray *)findAtleastTwoElementsPresentInTwoArray:(NSArray *)a bA:(NSArray *)b cA:(NSArray *)c{

    NSMutableArray *elements = [NSMutableArray array];

    id objcCommon1 = [a firstObjectCommonWithArray:b];
    id objcCommon2 = [b firstObjectCommonWithArray:c];
    id objcCommon3 = [c firstObjectCommonWithArray:a];

    if (objcCommon1){
        [elements addObject:objcCommon1];

    if (objcCommon2) {
        [elements addObject:objcCommon2];

    if (objcCommon3) {
       [elements addObject:objcCommon1];

    if (elements.count >= 2) {
        return elements;
        return nil;

Interview Candidate on 13 Aug 2015

- (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 10 Sep 2015

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.

Amit Priyadarshi on 12 Nov 2015

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


Shamsudheen TK on 21 Apr 2016

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

Oleh Kudinov on 26 Sep 2016

NSArray *a = @[@1, @2, @3];
    NSArray *b = @[@3, @4, @5];
    NSArray *c = @[@5, @6, @7];

    NSMutableSet *aSet = [NSMutableSet setWithArray:a];
    NSMutableSet *bSet = [NSMutableSet setWithArray:b];
    NSSet *cSet = [NSSet setWithArray:c];

    [aSet intersectSet:bSet];
    [bSet intersectSet:cSet];
    [aSet unionSet:bSet];

    NSLog(@"Final elements are:%@", aSet);

Avadesh Kumar on 1 Dec 2016

+ (NSArray*) findCommonElements:(NSArray*)a
                              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 4 Feb 2019

Add Answers or Comments

To comment on this, Sign In or Sign Up.