How to compare two lists

How to compare two lists

  

Hi, I'm making a mobile app. I would like to compare two lists with identical structure, with one particular list having all of the records and more than the other one, and somehow obtain a new list with the additional records that the smaller list does not have. How might I go about doing that, without using any forge component?

Hi Li Han,

Assuming that the lists are in the same order, I think the most straightforward thing is to iterate over the large list (with a For Each), and checking whether the Current element is equal to the pertaining element in the shorter list (keeping an index for the shorter list, and indexing it instead of using the Current). If not, it's an element only in the large list, so you can add that element to another List that keeps track of the additional records.

If the lists are not in the same order, use ListSort to sort them, or use the SortRecordList Forge component for more complex sorts. If sorting is not an option for whatever reason, comparison will be a lot slower. In that case you can iterate over the large lists and use a ListAny to check whether the element is present in the shorter list.

Solution

Hi Li Han,

In addition to the solution that Kilian provided, I want to suggest another.

If your "large" list is a lot more larger than the "shorter" list, you could, for sake of performance, choose to:

- iterate over the short list, and check if the element of the "short" list exists in the "large" list using ListIndexOf.

- save each returned index number in an long integer list

- duplicate the "large" list

- iterate the long integer list and do a delete ListRemove on the duplicated "large" list

- Now you have all the elements you are looking for

Regards,

Daniel




Solution

Hi Daniël,

I'm not sure whether that's more performant. Also, there's a flaw: since you are ListRemoving elements, the indexes saved in the Integer List should be corrected for that, or you'll end up removing the wrong elements (and possibly getting an Exception for removing an element that's not present).

Kilian Hekhuis wrote:

Hi Daniël,

I'm not sure whether that's more performant. Also, there's a flaw: since you are ListRemoving elements, the indexes saved in the Integer List should be corrected for that, or you'll end up removing the wrong elements (and possibly getting an Exception for removing an element that's not present).

Hi Kilian,

Regarding the performance, I was thinking in a use case where the large list has hundreds or thousands of records the the short list only a handful. Thinking that with the solution I propose prefer less iterations over a small list rather than many iterations over a large list. Anyhow I agree that one should only seek for a more optimized solution if there is really need too, and compare both solutions could give an answer to that.

You  are totally right regarding the flaw in my solution, should have thought a little bit more about it. It will work I if you would do the for each over the long integer list from back to front.

Regards,

Daniel


Hi, thank you Killian and Daniel for your ideas. Unfortunately, I can only mark one as an answer, but both methods are actually rather ingenious!