PDA

View Full Version : How to remove duplicate enteries from QStringList.



merry
11th December 2008, 08:59
Hi all

Working on Qt 4.4.2 on my intel mac machine.

Pls tell How to remove duplicate enteries from QStringList.

caduel
11th December 2008, 09:04
if you do not care about the order of the list:

QStringList sl = ...;
sl = sl.toSet().toList();

Maybe there are more efficient ways to do that.

Or you could do

for (int i=0; i<sl.count(); ++i)
{
forever { // remove all occurrences at greater indexes
int p=sl.lastIndexOf(sl.at[i]);
if (p==i) break;
sl.removeAt(p);
}
}
Untested, of course. Have, fun!

sherifomran
6th March 2019, 15:19
QStringList has a remove duplicates
QStringList::removeDuplicates() (https://doc.qt.io/archives/qt-4.8/qstringlist.html#removeDuplicates)

^NyAw^
6th March 2019, 16:21
Wow! Only 11 years to give us a solution.

d_stranz
6th March 2019, 21:00
Do you know how long I have been sitting here waiting for this answer? Now I can finally finish my project!

^NyAw^
7th March 2019, 15:02
Yes! I can sleep now.