c++ - Write the most efficient implementation of the function RemoveDuplication() -
so studying , have question , write efficient implementation of function removeduplication(), removes duplication in list. assume list sorted may have duplication. so, if list <2, 2, 5, 6, 6, 6, 9>, function should make <2, 5, 6, 9>.
the code thought of remove duplication right here , wanted know , if there more efficient ways of removing duplications in list
template <class t> void dllist<t>:: removeduplication() { for(dllnode<t>*ptr = head; ptr!=null; ptr=ptr->next) while (ptr->val == ptr->next->val) { ptr->next->next->prev = ptr; ptr->next = ptr->next->next; } }
it looks code run in o(n) algorithm. not going more efficient, because you'll have visit every item delete it.
if don't want delete duplicate objects though, want return new list containing non-duplicate objects, make faster making o(m) m amount of unique numbers, smaller or equal n. couldn't think way this.
recapping, possible faster, hard , improvement negligible.
ps. dont forget delete stuff when take out of list ;)
Comments
Post a Comment