Results 1 to 7 of 7

Thread: removing duplicate itens from QList<int>

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default removing duplicate itens from QList<int>

    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.
    Unless that there is some "magical" Qt function that I don't know about that does that for me

    Thanks
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: removing duplicate itens from QList<int>

    Search the QList doc for "duplicates".

  3. The following user says thank you to norobro for this useful post:

    john_god (22nd December 2010)

  4. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: removing duplicate itens from QList<int>

    Thanks norobro

    There is a
    int QStringList::removeDuplicates ()
    but only for QStringList
    I need it for QList, it seems it's not available
    Last edited by john_god; 22nd December 2010 at 21:05. Reason: updated contents
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  5. The following user says thank you to john_god for this useful post:

    Chromatix (4th September 2015)

  6. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: removing duplicate itens from QList<int>

    Won't QList::toSet() do what you want?

  7. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: removing duplicate itens from QList<int>

    ... and QSet::toList() to regain a QList. But maybe a small function of yourown to remove duplicates is faster.

  8. #6
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: removing duplicate itens from QList<int>

    Thank you guys

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

    Qt Code:
    1. QSet<int> set = m_hits_list.toSet();
    2. m_hits_list = set.toList();
    3. qSort(m_hits_list.begin(),m_hits_list.end());// sort the list
    To copy to clipboard, switch view to plain text mode 

    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.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  9. #7
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: removing duplicate itens from QList<int>

    If you need to remove duplicates, why allow the duplicates to the QList in the first place? Use QList::contains() to see if a value already exists in the list before adding it.

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  3. duplicate recordset
    By sepehr in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2009, 10:44
  4. Duplicate SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2008, 12:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.