Counting the frequency of an element in a list?

Is there a way to count how frequent an element in a list shows up?

For example if L=[ 1, 1, 1, 1, 1, 1, 3] and asked how many 1’s showed up, it would give me 6.

Thanks for any help!

https://www.desmos.com/calculator/ninnulcf2j

1 Like

If you’re wanting to do it in the CL in Activity Builder, you do something like:

List = [1, 1, 1, 1, 1, 1, 3]

num = 1

count = List.reduce(0, (acc,curr) => when curr = num numericValue(`${acc}+1`) otherwise acc)
1 Like

What’s the 0 in List.reduce(0, (acc,curr)? Is that the index of the list? I believe it is list.function(index, element), so maybe I just answered my own question.

For what I need right now I just need it in the graph. But this is really helpful!

1 Like

This is great! Thanks!

For most list functions, it’s list.function(element, index). For reduce, it’s list.reduce(starting value, (accumulator value, current value, current index).

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.