removing duplicate entries in a list.

A

Amit

Hi,
I have a list of integers. At each iteration, I remove some element
from it and then insert new elements in it. The order of elements is
not important. So I guess I could use a vector also for this purpose.
However, I am also interested in having no duplicacy in elements of
the vector. So I do not want to have any integer repeated more than
once in the list.
One very naive approach could be to compare the new integer being
added to every element of the list already present. Can I do any
better, by using sort() and unique() methods(in conjunction) in the
list? I am not aware of any such methods for a vector.. Any suggestions
which one might be better in terms of efficiency?

thanks,
--a.
 
F

Filip Dreger

Uzytkownik "Amit said:
Hi,
I have a list of integers. At each iteration, I remove some element
from it and then insert new elements in it. The order of elements is
not important. So I guess I could use a vector also for this
purpose.
However, I am also interested in having no duplicacy in elements of
the vector. So I do not want to have any integer repeated more than
once in the list.
One very naive approach could be to compare the new integer being
added to every element of the list already present. Can I do any
better, by using sort() and unique() methods(in conjunction) in the
list? I am not aware of any such methods for a vector.. Any
suggestions
which one might be better in terms of efficiency?
If the range of integers in question is limited and you want really
efficient 'sort' and 'unique' functions, you might be better off using
a bitfield with number of fields equal to the biggest integer you wish
to store. It takes quite a lot of memory (max. range of integers / 8
bytes) but gives you zero time 'sort' and zero time 'unique' functions
:)

regards,
Filip Dreger
 
C

Cy Edmunds

Amit said:
Hi,
I have a list of integers. At each iteration, I remove some element
from it and then insert new elements in it. The order of elements is
not important. So I guess I could use a vector also for this purpose.
However, I am also interested in having no duplicacy in elements of
the vector. So I do not want to have any integer repeated more than
once in the list.
One very naive approach could be to compare the new integer being
added to every element of the list already present. Can I do any
better, by using sort() and unique() methods(in conjunction) in the
list? I am not aware of any such methods for a vector.. Any suggestions
which one might be better in terms of efficiency?

thanks,
--a.

Have a look at std::set<int>.
 
R

Richard Herring

Amit said:
Hi,
I have a list of integers. At each iteration, I remove some element
from it and then insert new elements in it. The order of elements is
not important. So I guess I could use a vector also for this purpose.
However, I am also interested in having no duplicacy in elements of
the vector. So I do not want to have any integer repeated more than
once in the list.
One very naive approach could be to compare the new integer being
added to every element of the list already present. Can I do any
better, by using sort() and unique() methods(in conjunction) in the
list? I am not aware of any such methods for a vector..

That doesn't mean you can't do it: the reason there are no
vector-specific member functions for this is merely that the generic
(non-member) std:: algorithms will work on std::vector (unlike
std::list), so there's no point in having class-specific equivalents.
Any suggestions
which one might be better in terms of efficiency?

Since you're adding and removing all the time (which would entail
repeatedly re-sorting a vector) you might do better to use std::set,
which is intrinsically sorted already.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top