Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to find duplicates in QStringList
Forum Updated to NodeBB v4.3 + New Features

How to find duplicates in QStringList

Scheduled Pinned Locked Moved General and Desktop
5 Posts 5 Posters 11.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi All

    I have stored huge number of QStrings in QStringList.
    I want to throw error if there are duplicate strings in QStringList.
    What is the easy way to achieve this?
    I dont want to do it through for loop.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      One way to do that, would be to use a QSet<QString>. Just add all the items from the QStringList to the set like this:

      @
      QSet<QString> stringSet = QSet<QString>::fromList(myStringList);
      if (stringSet.count() < myStringList.count()) {
      //throw your error
      }
      @

      There are more efficient ways to do it, I guess. If your QStringList is sorted, then it becomes much easier, but you will have to loop over the list in one way or another. The code above loops over your list as well, but the loop is in the ::fromList call instead of in your own code.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cincirin
        wrote on last edited by
        #3

        Using a QSet<QString> is the faster method to search huge nnumber of strings.
        You can convert anyway into string list using QSet::toList.

        edit : ups, Andre has to fast :-)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          soroush
          wrote on last edited by
          #4

          I think it can not done faster than O(n²) for unsorted list. Even sorted lists can not reach to O(n×lg n) complexity because of O(n) for accessing elements of a list... Is this correct?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dangelog
            wrote on last edited by
            #5

            Index-based access for a QList is O(1).

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved