Count elements in an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rafal Konopka

    #1

    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


  • Janwillem Borleffs

    #2
    Re: Count elements in an array

    Rafal Konopka wrote:[color=blue]
    > 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.?
    >[/color]




    JW



    Comment

    • Janwillem Borleffs

      #3
      Re: Count elements in an array

      Janwillem Borleffs wrote:[color=blue]
      > http://www.php.net/manual/en/functio...unt-values.php
      >[/color]

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


      JW



      Comment

      • Ivo

        #4
        Re: Count elements in an array

        "Rafal Konopka" wrote[color=blue]
        > 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)[/color]
        the[color=blue]
        > array holds[/color]

        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



        Comment

        • Janwillem Borleffs

          #5
          Re: Count elements in an array

          Rafal Konopka wrote:[color=blue]
          > 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.?
          >[/color]

          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



          Comment

          • Rafal Konopka

            #6
            Re: Count elements in an array

            Thank,

            I'll give it a whirl and see what happens

            Rafal
            "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
            news:4234c12e$0 $38915$dbd4f001 @news.euronet.n l...[color=blue]
            > 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
            >
            >
            >[/color]


            Comment

            • Rafal Konopka

              #7
              Re: Count elements in an array

              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:4234bf38$0 $17382$dbd45001 @news.wanadoo.n l...[color=blue]
              > Several approaches exist. This one retuns an object with the array values[/color]
              as[color=blue]
              > 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/
              >
              >[/color]


              Comment

              • RobB

                #8
                Re: Count elements in an array

                Rafal Konopka wrote:[color=blue]
                > Ivo, It's a nice and crisp solution, but I cannot pass a parameter[/color]
                to the[color=blue]
                > 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[/color]
                value (its[color=blue]
                > count).
                >
                > Rafak
                >
                > "Ivo" <no@thank.you > wrote in message
                > news:4234bf38$0 $17382$dbd45001 @news.wanadoo.n l...[color=green]
                > > Several approaches exist. This one retuns an object with the array[/color][/color]
                values[color=blue]
                > as[color=green]
                > > 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/
                > >
                > >[/color][/color]

                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.

                Comment

                • RobG

                  #9
                  Re: Count elements in an array

                  Rafal Konopka wrote:[color=blue]
                  > 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.
                  >[/color]

                  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];
                  }
                  alert(msg);

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

                  </script>

                  --
                  Rob

                  Comment

                  • Rafal Konopka

                    #10
                    Re: Count elements in an array


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

                    Rafal


                    Comment

                    Working...