cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Distinct for sub-arrays

Suppose I have this collection: [[1, 2],[3, 4],[4,4],6]

I want to know the distinct element count of each subarray. So something like following:

subarray     | distinct_count
[1,2]        | 2
[3,4]        | 2
[4,4]        | 1
6            | 1

This doesn't give me the right result:

WITH [[1, 2],[3, 4],[4,4],6] AS nested
UNWIND nested AS x
RETURN x, count (distinct x)
1 ACCEPTED SOLUTION

Hi @SPK

I challenged this question.

WITH [[1, 2],[3, 4],[4,4],6] AS nested
UNWIND nested AS x
UNWIND x AS item
RETURN x AS subarray, count(distinct(item)) AS distinct_count

View solution in original post

4 REPLIES 4

jggomez
Graph Voyager

Hi, I think that it is not possible

WITH [[1, 2],[3, 4],[4,4],6] AS nested
UNWIND nested AS x
RETURN x, size(x)

Thanks

ameyasoft
Graph Maven
You are dealing with nested list and here is a solution.

WITH [[1, 2],[3, 4],[4,4],6] AS n1
return n1[0],  size(n1[0])  , n1[1], size(n1[1]), n1[2], size(n1[2]),
n1[3], size(n1[3])

Result:

Hi @SPK

I challenged this question.

WITH [[1, 2],[3, 4],[4,4],6] AS nested
UNWIND nested AS x
UNWIND x AS item
RETURN x AS subarray, count(distinct(item)) AS distinct_count

Thanks for all the answers. I also managed to solve this with

size(apoc.coll.toSet(item)) = 1

But answer by Koji is the most elegant I think.