Meta interview question

- Given an array, remove the duplicates and return a unique array keeping the first occurrence of the duplicates and the order. [@2, @1, @3, @1, @2] --> [@2, @1, @3] - how would you implement call for canceling queued blocks with dispatch_after?

Interview Answers

Anonymous

20 May 2014

There is a built in class that already does this. Just use that. NSArray *array; // input array NSArray *result = [[NSOrderedSet orderedSetWithArray:array] array];

11

Anonymous

8 Jul 2014

the question answered above are definitely not optimized but easy built in function, I doubt thats what the interviewer want, if it's sorted array, it will be easy, we simply keep track of the current number, remove duplicate and move forward, but if it's not sorted, then I think a NSMutableDictionary would be efficient to perform a one time scan from begin to the end, so it's O(n) time and O(n) space.

Anonymous

24 Sept 2015

Is it alright if we use NSOrderedSet? Or do they expect you to solve it without using the built-in components. Because with NSOrderedSet, it can be solved in 1 line.

Anonymous

17 Apr 2017

func removeDuplicates(ray:inout[Int])->[Int]{ ray = Array(Set(ray)) return ray } removeDuplicates(ray: &ray) OR func removeD(ray:[Int])->[Int]{ var ans=[Int]() var djSet=Set() for item in ray{ if djSet.contains(item){ continue }else{ djSet.insert(item) ans.append(item) } } return ans } var a = [2,1,3,5,1,2,5] removeD(ray: a)

Anonymous

4 Feb 2018

import Foundation func removeDuplicates(array: [Int]) -> [Int] { var set = Set() var result = Array() for number in array { if set.contains(number) { continue } set.insert(number) result.append(number) } return result } var initialArray = [2, 1, 3, 1, 2]; var finalArray = removeDuplicates(array: initialArray) print(finalArray) // [2, 1, 3]

Anonymous

26 May 2019

private var dismissNotificationTask: DispatchWorkItem? dismissNotificationTask = DispatchWorkItem { print("Hello") } if let dTask = dismissNotificationTask { let delay = DispatchTime.now() + .seconds(8) DispatchQueue.main.asyncAfter(deadline: delay, execute: dTask) } dismissNotificationTask?.cancel()

Anonymous

14 Nov 2014

I think Matteo's first solution is not correct. Because the question asked to keep the first occurrence, but solution 1 removed the first occurrence. Solution 2 is brilliant, btw.

Anonymous

25 Jan 2014

NSArray *input = @[@2, @1, @3, @1, @2]; NSArray *output = @[@2, @1, @3]; NSMutableArray *result = [NSMutableArray array]; for (id element in input) { if ([result containsObject:element] == NO) { [result addObject:element]; } } NSCAssert([result isEqualTo:output], @"fail");

1

Anonymous

25 Mar 2015

-(NSMutableArray*)arrayWithoutDuplicates:(NSArray*)array { NSMutableArray *resultArray = [[NSMutableArray alloc] init]; for (int i = 0; i < array.count; i++) { if ( [resultArray containsObject:array[i]] ) { NSLog(@"resultArray already has this object: %@", array[i]); } else [resultArray addObject:array[i]]; } return resultArray; }

Anonymous

11 Dec 2013

Is the array sorted? If so, sounds similar to oj.leetcode.com/problems/remove-duplicates-from-sorted-array/

Anonymous

1 Aug 2014

your new array=[[NSSet setWithArray:yourOldArray] allObjects];

1

Anonymous

23 Nov 2013

Sorry, what was you response at the first question? Is banal, but in Objective-C there are different solution.. I write 2 solution very fast, but the second is exponential faster then the first one. //First solution (1 to 1) - (NSMutableArray *)removeDuplicateFromArray:(NSMutableArray *)arr { if([arr count] = 0 && pos < i) { [arr removeObjectAtIndex:i]; i--; } } return arr; } //Other solution - (NSMutableArray *)removeDuplicateInGroupFromArray:(NSMutableArray *)arr { if([arr count] < 2) return arr; for(int i=0; i<[arr count]-1; i++) { [arr removeObject:arr[i] inRange:NSMakeRange(i+1, [arr count]-i-1)]; } return arr; }

2