Is there a method in Ruby that takes an array, and counts all unique elements and their occurrences and passes them back as a hash?

For example

  ['A','A','A','A','B','B','C'].method
> {'A' => 4, 'B' => 2, 'C' => 1}

Something like that.

['A','A','A','A','B','B','C'].group_by{|e| e}.map{|k, v| [k, v.length]}.to_h

src = ['A','A','A','A','B','B','C']
Hash[src.group_by { |x| x }.map { |k, v| [k, v.length] }]
counts = Hash.new(0)
['A','A','A','A','B','B','C'].each { |name| counts[name] += 1 }
counts => {"A"=>4, "B"=>2, "C"=>1}

['A','A','A','A','B','B','C'].each_with_object(Hash.new(0)) { |l, o| o[l] += 1 }

This is the easiest readable for me:

src = ['A','A','A','A','B','B','C']
src.group_by(&:to_s).to_a.map { |a| [a[0], a[1].count] }.to_h

Or here is another solution with reduce method:

src.reduce({}) { |b, a| b.merge({a => (b[a] || 0) + 1}) }

Or:

src.reduce(Hash.new(0)) { |b, a| b.merge({a => b[a] + 1}) }

Ruby: Count unique elements and their occurences in an array的更多相关文章

  1. [geeksforgeeks] Count the number of occurrences in a sorted array

    Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...

  2. FB面经 Prepare: Count Unique Island

    数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right ...

  3. leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;

    ,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...

  4. [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search

    Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...

  5. numpy.unique

    Find the unique elements of an array. Returns the sorted unique elements of an array. There are thre ...

  6. [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  7. Leetcode 347. Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  8. 【ruby】ruby基础知识

    Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...

  9. Non-unique Elements

    Non-unique Elements You are given a non-empty list of integers (X). For this task, you should return ...

随机推荐

  1. asp.net在用户控件中使用ClientScript

    在用户空间中调用ClientScript.RegisterClientScriptBlock方法 ClientScript的命名空间是System.Web.UI.Page,并且要实例化之后的Page才 ...

  2. IOS开展:导航中添加多个button并加入左侧logo

    添加多个button,同样只能加入一个 UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:NSLocali ...

  3. 离PACKET_INp获取信息acket data

         于Floodlight模块假设要packet in消息,就对对应的消息类型进行监听就可以.然后在receive方法中就能够操纵这个上传上来的packet_in.      关键代码:   E ...

  4. ReactJS学习 相关网站

    React 入门实例教程-阮一峰 http://www.ruanyifeng.com/blog/2015/03/react.html汇智网-React 互动学习http://hubwiz.com/co ...

  5. 最简单的视音频播放演示样例8:DirectSound播放PCM

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

  6. cocos2dx-2.x CCFileUtils文件管理分析(2)

    于1于,我只是对整体结构进行了分析,然后,2于,我会在一些我们经常使用的分析功能. //获取给定文件名称的全路径 //以下这非常长一段凝视.通过举样例,像我们说明cocos2dx获取文件全路径的规则. ...

  7. JAVA Metrics 度量工具使用介绍1

    Java Metric使用介绍1 Metrics是一个给JAVA提供度量工具的包,在JAVA代码中嵌入Metrics代码,可以方便的对业务代码的各个指标进行监控,同一时候,Metrics可以非常好的跟 ...

  8. net搭建热插拔式web框架(沙箱的构建)

    net搭建热插拔式web框架(沙箱的构建) 上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章 ...

  9. 在 Swift 语言中更好的处理 JSON 数据:SwiftyJSON

    SwiftyJSON能够让在Swift语言中更加简便处理JSON数据. With SwiftyJSON all you have to do is: ? 1 2 3 4 let json = JSON ...

  10. 轻松搭建Windows8云平台开发环境

    原文:轻松搭建Windows8云平台开发环境 Windows Store应用是基于Windows 8操作系统的新一代Windows应用程序,其开发平台以及运行模式和以往传统平台略有不同.为了帮助更多开 ...