SPK
(SPK)
December 3, 2020, 11:53pm
1
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)
jggomez
(Juan Guillermo Gómez)
December 4, 2020, 2:53am
2
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
( Ameyasoft)
December 4, 2020, 5:07am
3
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:
koji
(Koji Annoura)
December 4, 2020, 5:24am
4
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
SPK
(SPK)
December 11, 2020, 4:18pm
5
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.