Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26670

Re: function to get frequencies of a char array

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: function to get frequencies of a char array
Date 2015-01-23 16:52 +0100
Organization PointedEars Software (PES)
Message-ID <26839541.MRbOrngyXZ@PointedEars.de> (permalink)
References <dec04bcb-c8c2-4446-b82c-6c3fa7e05e48@googlegroups.com> <XnsA42B6242E020eejj99@194.109.133.133> <m9ti7r$sbg$1@solani.org>

Show all headers | View raw


Christoph M. Becker wrote:

> Evertjan. wrote:
>> Try this, using an object as target:
>> 
>> =============================
>> var arr = 'abjcdjjj'.split(''); // source array
>> var res = {};                   // result object
>> 
>> function frequencies() {
>>   for(i=0; i< arr.length; i++) {
>>     if (!res[arr[i]])
>>       res[arr[i]] = 0;
>>     res[arr[i]] +=1;
>>   };
>> };
>> frequencies();
>> 
>> console.log(res); // Object {a: 1, b: 1, j: 4, c: 1, d: 1}
>> =============================
> 
> Or maybe using Array.prototype.reduce:
> 
>     var a = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>     a.reduce(function (prev, curr) {
>         if (prev.hasOwnProperty(curr)) {
>             prev[curr]++;
>         } else {
>             prev[curr] = 1;
>         }
>         return prev;
>     }, {});

While this works – but I would use “Object.create(null)” instead of “{}” to 
avoid name collisions –, I find a “for” or “for–in” statement more obvious 
and compatible:

  var counts = {};

  for (var i = 0, len = a.length; i < len; ++i)
  {
    /* NOTE: hasOwnProperty() may be required */
    var e = a[i];
    e ? ++counts[e] : (counts[e] = 1);
  }

  for (var i in a)
  {
    /* NOTE: hasOwnProperty() may be required */
    var e = a[i];
    e ? ++counts[e] : (counts[e] = 1);
  }

“for–in” is more efficient than “for“ and Array.prototype.forEach() with 
sparse arrays.

Array.prototype.forEach() can be used instead if it is supported and the 
array is insufficiently sparse:

  a.forEach(function (e, i, a) {
    var counts = a.counts_ || (a.counts_ = Object.create(null));
    (counts[e]) ? ++counts[e] : (counts[e] = 1);
  });
  
  a.counts_

The advantage of using the Array instance as container is that the scope 
chain is shorter, which is faster.  However, that method, too, can be passed 
a “this” value:

  var counts = Object.create(null);

  a.forEach(
    function (e) { (this[e]) ? ++this[e] : (this[e] = 1); },
    counts);
  
  counts

Array.prototype.reduce(), if I use it (it needs to be emulated in case it is 
not available), I reserve for getting a single *primitive* value from an 
Array instead.  For example:

  var sum = a.reduce(function (prev, curr) { return prev + (+curr); }, 0);

(Implicit type conversion is unnecessary if all elements are of type 
Number.)

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 14:43 -0800
  Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-22 23:09 +0000
    Re: function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 15:22 -0800
  Re: function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 15:23 -0800
  Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 00:36 +0100
    Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 14:27 +0100
      Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-23 16:52 +0100
        Re: function to get frequencies of a char array Scott Sauyet <scott.sauyet@gmail.com> - 2015-01-23 09:03 -0800
        Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 23:03 +0100
        Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-24 14:29 +0000
          Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-24 15:36 +0100
            Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-24 16:06 +0100
              Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-24 18:05 +0100
                Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 10:21 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-26 13:22 +0100
                Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 15:50 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-26 20:48 +0100
                Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 23:08 +0100
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 01:05 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-27 13:55 +0100
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 14:50 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-27 19:35 +0100
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 22:15 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-31 00:40 +0100
            Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-24 17:49 +0000
              Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 10:23 +0100
              Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 01:00 +0100
                Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-27 01:37 +0000
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 03:03 +0100
    Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 15:29 +0100
  Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 02:16 +0000
    Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 02:18 +0000
    Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 09:06 +0100
      Re: function to get frequencies of a char array Stefan Weiss <krewecherl@gmail.com> - 2015-01-23 09:53 +0100
        Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 13:37 +0100
          Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 13:49 +0100
    Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 13:31 +0100
    Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 14:49 +0000
    Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-23 17:09 +0100

csiph-web