R
Rafal Konopka
How can you caount the number of occurrences of elements in an array? For
example
var arr1 =
[1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2,5,1,4,2,4,2,3,2,4,1];
how can I count how many 1s, 2s, 3s, etc.?
Despite the example, I never actually know what elements (and how many) the
array holds, which means, that the following is NOT a solution for me:
for( var i =0; i <arr1.length; i++) {
if (arr1 ==1) cnt1++;
if (arr1 ==2) cnt2++;
etc.
}
Thanks,
Rafal
example
var arr1 =
[1,5,1,1,5,2,4,5,2,3,2,4,2,4,2,3,2,3,2,4,2,3,2,4,2,5,1,4,2,4,2,3,2,4,1];
how can I count how many 1s, 2s, 3s, etc.?
Despite the example, I never actually know what elements (and how many) the
array holds, which means, that the following is NOT a solution for me:
for( var i =0; i <arr1.length; i++) {
if (arr1 ==1) cnt1++;
if (arr1 ==2) cnt2++;
etc.
}
Thanks,
Rafal