Count elements in an array

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
 
I

Ivo

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];
b[a] = j ? j+1 : 1;
}
return b;
}

var arr=['you','you','you','me','me','them','them','others'];
alert( countvalues(arr)['you'] ); // 3

hth
 
J

Janwillem Borleffs

Rafal said:
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]) {
count[arr1] += 1;
} else {
count[arr1] = 1;
}
}

for (i in count) msg += i + ' occurres ' + count + ' times\n';
alert(msg);


JW
 
R

Rafal Konopka

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
 
R

RobB

Rafal said:
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 said:
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];
b[a] = j ? j+1 : 1;
}
return b;
}

var arr=['you','you','you','me','me','them','them','others'];
alert( countvalues(arr)['you'] ); // 3

hth


Maybe...

Array.prototype.countvalues = function(k)
{
var b = {}, i = this.length, j;
while( i-- )
{
j = b[this];
b[this] = 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.
 
R

RobG

Rafal said:
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];
b[a] = 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>
 
R

Rafal Konopka

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

Rafal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top