473,581 Members | 2,488 Online

# Count elements in an array

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[i] ==1) cnt1++;
if (arr1[i] ==2) cnt2++;
etc.
}

Thanks,

Rafal
Jul 23 '05 #1
9 110925
Rafal Konopka wrote:
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.?

http://www.php.net/manual/en/functio...unt-values.php
JW

Jul 23 '05 #2
Janwillem Borleffs wrote:
http://www.php.net/manual/en/functio...unt-values.php

Sorry, thought I was reading a PHP newsgroup...
JW

Jul 23 '05 #3
"Rafal Konopka" wrote
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

Several approaches exist. This one retuns an object with the array values as
keys and the number of times they occur as values:

function countvalues(a) {
var b = {}, i = a.length, j;
while( i-- ) {
j = b[a[i]];
b[a[i]] = j ? j+1 : 1;
}
return b;
}

var arr=['you','you','yo u','me','me','t hem','them','ot hers'];
alert( countvalues(arr )['you'] ); // 3

hth
--
Ivo
http://4umi.com/
Jul 23 '05 #4
Rafal Konopka wrote:
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.?

The following might be useful:

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];
var count = [];
var msg = '';

for (var i = 0; i < arr1.length; i++) {
if (count[arr1[i]]) {
count[arr1[i]] += 1;
} else {
count[arr1[i]] = 1;
}
}

for (i in count) msg += i + ' occurres ' + count[i] + ' times\n';
JW

Jul 23 '05 #5
Thank,

I'll give it a whirl and see what happens

Rafal
"Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
news:42******** *************** @news.euronet.n l...
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];
var count = [];
var msg = '';

for (var i = 0; i < arr1.length; i++) {
if (count[arr1[i]]) {
count[arr1[i]] += 1;
} else {
count[arr1[i]] = 1;
}
}

for (i in count) msg += i + ' occurres ' + count[i] + ' times\n';
JW

Jul 23 '05 #6
Ivo, It's a nice and crisp solution, but I cannot pass a parameter to the
function as I never know what elements the array holds.

something similar to (and oh-so-useful) Perl's

\$count{\$val}++;

vivifying the hash by creating the key (the unknown element) and value (its
count).

Rafak

"Ivo" <no@thank.you > wrote in message
Several approaches exist. This one retuns an object with the array values as keys and the number of times they occur as values:

function countvalues(a) {
var b = {}, i = a.length, j;
while( i-- ) {
j = b[a[i]];
b[a[i]] = j ? j+1 : 1;
}
return b;
}

var arr=['you','you','yo u','me','me','t hem','them','ot hers'];
alert( countvalues(arr )['you'] ); // 3

hth
--
Ivo
http://4umi.com/

Jul 23 '05 #7
Rafal Konopka wrote:
Ivo, It's a nice and crisp solution, but I cannot pass a parameter to the function as I never know what elements the array holds.

something similar to (and oh-so-useful) Perl's

\$count{\$val}++;

vivifying the hash by creating the key (the unknown element) and value (its count).

Rafak

"Ivo" <no@thank.you > wrote in message
Several approaches exist. This one retuns an object with the array
values as
keys and the number of times they occur as values:

function countvalues(a) {
var b = {}, i = a.length, j;
while( i-- ) {
j = b[a[i]];
b[a[i]] = j ? j+1 : 1;
}
return b;
}

var arr=['you','you','yo u','me','me','t hem','them','ot hers'];
alert( countvalues(arr )['you'] ); // 3

hth
--
Ivo
http://4umi.com/

Maybe...

Array.prototype .countvalues = function(k)
{
var b = {}, i = this.length, j;
while( i-- )
{
j = b[this[i]];
b[this[i]] = j ? j+1 : 1;
}
return b[k];
}

var arr=['you','you','yo u','me','m*e',' them','them','o thers'];
alert( arr.countvalues ('you') ) // 3

This won't distinguish between string/numeric primitives ('4' / 4)
however; don't think you can hash with these as JS objects treat
string/numeric integer subscripts similarly.

Jul 23 '05 #8
Rafal Konopka wrote:
Ivo, It's a nice and crisp solution, but I cannot pass a parameter to the
function as I never know what elements the array holds.

If you just want to get a reduced array with unique elements and
the count of occurrences, then Ivo's solution actually does
exactly that. You just need to modify the output:

<script type="text/javascript">
function countvalues(a) {
var b = {}, i = a.length, j;
while( i-- ) {
j = b[a[i]];
b[a[i]] = j ? j+1 : 1;
}
return b; // an object of element:count arrays
}

var arr=['you','you','me ','you',
'me','them','th em',
'others','you'];

var a = countvalues(arr );
var msg='';

// To see how many times each element appears
for (elem in a){
msg += '\n' + elem + ' : ' + a[elem];
}

// To see how many times a particular element appears
alert( countvalues(arr )['you'] ); // 4

</script>

--
Rob
Jul 23 '05 #9

Thanks to all who replied to my question (counting elements of an array).
It was in fact very educational.

Rafal
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.