Search code examples
cyphergraph-databasesmemgraphdb

How can I return two lists that are DISTINCT?


How can I return two lists that are DISTINCT? I use UNWIND clause to do that with one, but the problem arises when I want to have two independent lists with DISTINCT elements.

Here is my code:

WITH [1,1,1,2,2,3]AS list, [2,3,4,5,6,7,1,2,1]as list2
UNWIND listAS listElement
UNWIND list2AS listElement2
WITHDISTINCT listElement, listElement2
RETURN collect(listElement)AS distinctElements, collect(listElement2)AS distinctElements2;

I want to get two lists with DISTINCT elements, and now I get duplicates. What am I doing wrong?


Solution

  • You can combine both lists in one list and then check if the elements of one list are in that combo list. If they are, they are from the first list, if not, they are from the second one.

    On top of that, you can unwind lists one by one:

    WITH [1,1,1,2,2,3] AS list, [2,3,4,5,6,7,1,2,1] AS list2
    UNWIND list AS listElement
    WITH DISTINCT listElement, list2
    WITH collect(listElement) as list, list2
    UNWIND list2 as listElement
    WITH DISTINCT listElement, list
    RETURN list, collect(listElement) AS list2;