By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,769 Members | 1,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,769 IT Pros & Developers. It's quick & easy.

Count elements in an array

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
Ivo
"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','you','me','me','them','them','others'];
alert( countvalues(arr)['you'] ); // 3

hth
--
Ivo
http://4umi.com/
Jul 23 '05 #4

P: n/a
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';
alert(msg);
JW

Jul 23 '05 #5

P: n/a
Thank,

I'll give it a whirl and see what happens

Rafal
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42***********************@news.euronet.nl...
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';
alert(msg);
JW

Jul 23 '05 #6

P: n/a
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.

Essentiall, I was wondering if the newer versions of javascript had
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
news:42***********************@news.wanadoo.nl...
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','you','me','me','them','them','others'];
alert( countvalues(arr)['you'] ); // 3

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

Jul 23 '05 #7

P: n/a
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.

Essentiall, I was wondering if the newer versions of javascript had
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
news:42***********************@news.wanadoo.nl...
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','you','me','me','them','them','others'];
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','you','me','m*e','them','them','others '];
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

P: n/a
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','them',
'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];
}
alert(msg);

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

</script>

--
Rob
Jul 23 '05 #9

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.