Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>ID = -&gt; x { x } # Why is the identity function not in the core lib? f = &lt;&lt;-HERE 0101 1010 1311 0101 1311 431 1010 431 420 HERE Hash[f.lines.map(&amp;:to_i).group_by(&amp;ID).map {|n, ns| [n, ns.size] }] # { 101 =&gt; 2, 1010 =&gt; 2, 1311 =&gt; 2, 431 =&gt; 2, 420 =&gt; 1 } </code></pre> <p>You simply group the numbers by themselves using <code>Enumerable#group_by</code>, which gives you something like</p> <pre><code>{ 101 =&gt; [101, 101], 420 =&gt; [420] } </code></pre> <p>And then you <code>Enumerable#map</code> the value arrays to their lengths, i.e. <code>[101, 101]</code> becomes <code>2</code>. Then just convert it back to a <code>Hash</code> using <code>Hash::[]</code>.</p> <p>However, if you are willing to use a third-party library, it becomes even more trivial, because if you use a <code>MultiSet</code> data structure, the answer falls out naturally. (A <code>MultiSet</code> is like a <code>Set</code>, except that an item can be added multiple times and the <code>MultiSet</code> will keep count of how often an item was added&nbsp;&ndash; which is exactly what you want.)</p> <pre><code>require 'multiset' # Google for it, it's so old that it isn't available as a Gem Multiset[*f.lines.map(&amp;:to_i)] # =&gt; #&lt;Multiset:#2 101, #2 1010, #2 1311, #2 431, #1 420&gt; </code></pre> <p>Yes, that's it.</p> <p>That's the beatiful thing about using the right datastructure: your algorithms become massively simpler. Or, in this particular case, the algorithm just <em>vanishes</em>.</p> <p>I've written more about using <code>MultiSet</code>s for solving this exact problem at</p> <ul> <li><a href="http://StackOverflow.Com/q/4231534/#4232851">find the repetition of duplicate numbers.</a> (the first example in that post is the same as the second one here, the second example in that post is essentially the same as @Chuck's here and the third one is the same as the first in this post.)</li> <li><a href="http://StackOverflow.Com/q/3735605/#3737892">Counting occurences of unique letters in a string</a> (the Ruby example is the same as here, the Scala and ECMAScript examples are the same as @Chuck's and the C# example is the same as my <code>group_by</code> example here.)</li> <li><a href="http://Article.GMaNe.Org/gmane.comp.lang.scala.user/33698/" rel="nofollow">Bag/Multiset in scala and a touch of multimap</a> (Again the same three examples as my two and @Chuck's in Scala.)</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload