-
Notifications
You must be signed in to change notification settings - Fork 664
How to count the number of times each value in the array appears in that array
Source: StackOverflow.com
I have an array of values. Essentially, I'd like to count the number of times each value in the array appears in that array. So for instance at a particular pixel of the world, the array might look like:
[1, 1, 0.7, 0.3, 1, 0.8, 1, 0.8, 0.7, 1]
I need to count the number of times a particular value appears, then run the expression
Expression(value*(value-1)(value2+5));
So for that array, I want the distinct value counts...
1: 5
0.7: 2
0.8: 2
0.3: 1
And then compute the value of those "ties" as a part of the expression...
5 * (5-1) * (5*2 -5) +
2 * (2-1) * (2*2 -5) +
2 * (2-1) * (2*2 -5) +
1 * (1-1) * (1*2 -5)
As seen above, values with a count of 1 fall out, which is fine, however, I need to avoid double counting items so simply transforming the array into counts ([5, 5, 2, 1, 5, 2, 5, 2, 2, 5]
) is incorrect.
AlaSQL library was originally designed to make calculations like in you question.
var arr = [1, 1, 0.7, 0.3, 1, 0.8, 1, 0.8, 0.7, 1];
var res = alasql('SELECT VALUE SUM(v*(v-1)*(v*2+5)) FROM
(SELECT COUNT(*) AS v FROM ? GROUP BY _)',[arr]);
Try this example in jsFIddle
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo