Pergunta de entrevista da empresa Meta

- 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?

Respostas da entrevista

Sigiloso

20 de mai. de 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

Sigiloso

8 de jul. de 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.

Sigiloso

24 de set. de 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.

Sigiloso

17 de abr. de 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)

Sigiloso

4 de fev. de 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]

Sigiloso

26 de mai. de 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()

Sigiloso

14 de nov. de 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.

Sigiloso

25 de jan. de 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

Sigiloso

25 de mar. de 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; }

Sigiloso

11 de dez. de 2013

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

Sigiloso

1 de ago. de 2014

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

1

Sigiloso

23 de nov. de 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