View Full Version : removing duplicate itens from QList<int>

22nd December 2010, 21:28
Hi guys
I'm using qSort () to sort a QList<int> list.
Now I will hardcode it to remove any duplicate int's from QList. :cool:
Unless that there is some "magical" Qt function that I don't know about that does that for me :confused:

Thanks :)

22nd December 2010, 21:51
Search the QList doc for "duplicates".

22nd December 2010, 21:55
Thanks norobro

There is a
int QStringList::removeDuplicates ()
but only for QStringList
I need it for QList, it seems it's not available :(

22nd December 2010, 22:34
Won't QList::toSet() do what you want?

22nd December 2010, 22:47
... and QSet::toList() to regain a QList. But maybe a small function of yourown to remove duplicates is faster.

22nd December 2010, 23:02
Thank you guys

I will check this later, but something like this should work

QSet<int> set = m_hits_list.toSet();
m_hits_list = set.toList();
qSort(m_hits_list.begin(),m_hits_list.end());// sort the list

But maybe a small function of yourown to remove duplicates is faster.
Do you think Lykurg ? I will check for performance later, but I thought Qt functions were very optimized. I would problably do it with a for() and a removeAll(), don't know if would be faster.

7th August 2014, 07:56
If you need to remove duplicates, why allow the duplicates to the QList in the first place? Use QList::contains() (http://qt-project.org/doc/qt-5/qlist.html#contains) to see if a value already exists in the list before adding it.