Cypher DISTINCT for lists clause

My question is how can I return two lists that are DISTINCT, it is straightforward (UNWIND clause | Memgraph Docs) easy to do that with one, bu the problem arises when I want to have two independent lists with DISTINCT elements. e.g.

WITH [1,1,1,2,2,3] AS list, [2,3,4,5,6,7,1,2,1] as list2
UNWIND list AS listElement
UNWIND list2 AS listElement2
WITH DISTINCT 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…

How would one solve that? neo4j support DISTINCT inside the collect…

By combining both lists in one list and then asking 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 :smiley:

Also 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;
1 Like