Pergunta de entrevista da empresa Microsoft

best strategy to take out the duplicate elements in an array

Resposta da entrevista

Sigiloso

29 de fev. de 2012

set seen; for( element in array): if( seen.contains( element ) ): array.erase( element ) Runtime: * For a tree based set, O( n * log n ) * For a hash table based set, O( n ) assuming no collisions and constant hashing Improvements: If array.erase isn't efficient (it's an array, it won't be) then keep a collection of pointers or indices into the array to erase in a more efficient manner than 1 at a time.