# Thread: How to compare 2 Lists?

1. ## How to compare 2 Lists?

Hi,

I have two lists, QList<int>list1;and QList<int>list2; List1 have 20 elements and list2 have 10 elements. I want to compare these two list and also want to store the matched elements in third list.

Thanks,
Monali.

2. ## Re: How to compare 2 Lists?

You can get idea how to do that from this sample program
http://www.experts-exchange.com/Prog..._22974817.html
Complexity of algorithm is O(M * N), however, it can be reduced to O(M + N) in case lists are sorted.

3. ## Re: How to compare 2 Lists?

In case you are looking for a way to do this with a Qt call: forget it.

I suggest you just u8se two for() lists, especially as the lists are so small...

4. ## Re: How to compare 2 Lists?

Well, you either compare every element in one to every element in the other, with two nested loops, or you use qSort to sort the lists and step through them in sync with each other.

Either way requires some actual programming skill, I'm afraid -- no cookbook.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•