Domanda di colloquio di Microsoft

best strategy to take out the duplicate elements in an array

Risposta di colloquio

Anonimo

29 feb 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.