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


Groups > comp.lang.javascript > #26726

Re: function to get frequencies of a char array

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: function to get frequencies of a char array
Date Tue, 27 Jan 2015 03:03:05 +0100
Organization PointedEars Software (PES)
Lines 68
Message-ID <1859346.AqY2EoPZMB@PointedEars.de> (permalink)
References <dec04bcb-c8c2-4446-b82c-6c3fa7e05e48@googlegroups.com> <XnsA42B6242E020eejj99@194.109.133.133> <m9ti7r$sbg$1@solani.org> <26839541.MRbOrngyXZ@PointedEars.de> <87twzgl0aq.fsf@bsb.me.uk> <2697158.aGRKFDBAEn@PointedEars.de> <87lhkskr0r.fsf@bsb.me.uk> <4289032.9WYFBcEGMJ@PointedEars.de> <87twzdf1gu.fsf@bsb.me.uk>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1422324224 29758 eJwNwoERgEAIA7CVUNoK48AL+4+gl9B16TwQBe4PztOL4PSGK8YmmKrJWpuMGy96nWZR8P4AHlsRFA== (27 Jan 2015 02:03:44 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Tue, 27 Jan 2015 02:03:44 +0000 (UTC)
User-Agent KNode/4.14.2
X-User-ID eJwVyMERACEIBLCWRNgVywGB/ku4uTwDpfAdI2gYjJ7qLr8REiOdC34al657e9eWHD6DSC23mJT/PLCYpUH7AGURFbg=
Cancel-Lock sha1:wZvWkFzATLcbGZdKCsB/b40RMvo=
X-NNTP-Posting-Host eJwFwYERADAEBLCVKP4Zp1ztP0KTMCiGjoDHxs5t5tP7tDLMzYd7CN2Sdno3xEjp5NjB1gcXORB+
Xref csiph.com comp.lang.javascript:26726

Show key headers only | View raw


Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>> Ben Bacarisse wrote:
>>>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>>>>   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);
>>>>>>   }
>>>>> 
>>>>> I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and
>>>>> immediately below): […]
>>>>
>>>> No, but
>>>>
>>>>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
>>> 
>>> Why?
>>
>> Because there are sparse arrays ([To]String(“undefined”) ===
>> "undefined").
> 
> Isn't
> 
>   if (a.hasOwnProperty(i)) { counts[e] ? ++counts[e] : (counts[e] = 1); }
> 
> a better way to deal with sparse arrays in such a loop?

In general, it is a better way.  However, since Array indexes are enumerable 
properties, you can also iterate over a filtered, sorted Object.keys(a), say

  var indexes = Object.keys(a).filter(function (e) {
    return +e >= 0 && +e < Math.pow(2, 32);
  }).sort(function (a, b) {
    return a - b;
  });

  for (var ii = 0, len = indexes.length; ii < len; ++ii)
  {
    var e = a[indexes[ii]];
    // …
  }

Which approach is more efficient depends on the sparseness of the array; the 
latter solves the problem that iteration order is unspecified with “for–in” 
and includes inherited properties.

That means you could do

  var reduced = Object.keys(a).filter(…).sort(…).map(…).reduce(…);

> Testing e seems rather heavy-handed, though it's not clear what the OP 
> might want in anything but the simple case given.  In a general counting 
> loop one would probably want to count zeros and NaNs and so on.

Yes, as so often, this is limited to the use-case.

-- 
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