{"id":1683,"date":"2012-12-05T18:28:11","date_gmt":"2012-12-05T23:28:11","guid":{"rendered":"http:\/\/www.rexfeng.com\/blog\/?p=1683"},"modified":"2012-12-05T18:28:11","modified_gmt":"2012-12-05T23:28:11","slug":"how-to-count-ruby-array-items","status":"publish","type":"post","link":"https:\/\/www.rexfeng.com\/blog\/2012\/12\/how-to-count-ruby-array-items\/","title":{"rendered":"How to count Ruby Array items"},"content":{"rendered":"<p>While working in Ruby, I often find myself trying to get a count of the number of times each item appears in an array. Below, I&#8217;ll show you how to create a Hash that iterates through each item in the array and counts the number of occurrences.<\/p>\n<p>We have an array of [cci]animals[\/cci] that we want to count.<\/p>\n<p>[cc]animals = [&#8220;tiger&#8221;,<br \/>\n           &#8220;tiger&#8221;,<br \/>\n           &#8220;lion&#8221;,<br \/>\n           &#8220;dog&#8221;,<br \/>\n           &#8220;cat&#8221;,<br \/>\n           &#8220;mouse&#8221;,<br \/>\n           &#8220;bear&#8221;,<br \/>\n           &#8220;frog&#8221;,<br \/>\n           &#8220;tiger&#8221;,<br \/>\n           &#8220;lion&#8221;,<br \/>\n           &#8220;frog&#8221;,<br \/>\n           &#8220;fish&#8221;,<br \/>\n           &#8220;cat&#8221;,<br \/>\n           &#8220;dog&#8221;,<br \/>\n           &#8220;cat&#8221;<br \/>\n          ][\/cc]<br \/>\nWe can count them up by creating a new Hash.<\/p>\n<p>[cc]animal_count = Hash.new(0)[\/cc]<\/p>\n<p>Next, we want to iterate through the [cci]animals[\/cci] Array and set the [cci]animal_count[\/cci] Key as the animal name &amp; the [cci]animal_count[\/cci] Value as the counter.<\/p>\n<p>[cc]animals.each { |animal| animal_count[animal] += 1 }[\/cc]<\/p>\n<p>For each [cci]animal[\/cci] in [cci]animals[\/cci], we&#8217;ve either created a new Key \/ Value pair in [cci]animal_count[\/cci] or incremented the Value corresponding to the [cci]animal[\/cci] Key.<br \/>\n[cc]puts animal_count<br \/>\n#=> {&#8220;tiger&#8221;=>3, &#8220;lion&#8221;=>2, &#8220;dog&#8221;=>2, &#8220;cat&#8221;=>3, &#8220;mouse&#8221;=>1, &#8220;bear&#8221;=>1, &#8220;frog&#8221;=>2, &#8220;fish&#8221;=>1} [\/cc]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While working in Ruby, I often find myself trying to get a count of the number of times each item appears in an array. Below, I&#8217;ll show you how to create a Hash that iterates through each item in the array and counts the number of occurrences. We have an array of [cci]animals[\/cci] that we [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1029],"tags":[1036,1037,1040,1038,1041,1039,332],"class_list":["post-1683","post","type-post","status-publish","format-standard","hentry","category-programming","tag-array","tag-count","tag-each","tag-hash","tag-increment","tag-iterate","tag-ruby"],"_links":{"self":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/1683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/comments?post=1683"}],"version-history":[{"count":9,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/1683\/revisions"}],"predecessor-version":[{"id":1692,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/1683\/revisions\/1692"}],"wp:attachment":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/media?parent=1683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/categories?post=1683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/tags?post=1683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}