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. Java 抽象工厂模式

    抽象工厂模式(Abstract Factory Pattern)是工厂方法模式的进一步抽象,其英文原话"Provide an interface for creating families ...

  2. python 调用图灵机器人api实现简单的人机交互

    接入流程例如以下,须要先注冊开发人员帐号,之后会得到一个32位的key,保存下来,用于以后发送数据.http://www.tuling123.com/ 请求方式 演示样例: # -*- coding: ...

  3. SpringMVC 上下文webApplicationContext

    使用listener听众载入配置,一般Struts+Spring+Hibernate是使用listener监听器的.例如以下 <listener> <listener-class&g ...

  4. 【SICP感应】1 工艺和替代模式

    <计算机程序的构造和解释>这本书的目的不是要解释的编程语言的语法,它是一种. 不是在你的语句知识,但是,你要教的东西做什么. 这是一个过程,一个精神. 就是所谓的程序规则的模式. 书中用了 ...

  5. Ubuntu 13.04 用户安装 gnome 3.8 桌面

    昨天我试用了一把 Ubuntu gnome 13.04,如果你看了那一片文章:Ubuntu Gnome 13.04 体验截图.对 Ubuntu gnome 13.04 并不是采用的gnome 3.8 ...

  6. linux、hdfs、hive、hbase经常使用的命令

    linux经常使用命令 pwd 查看当前工作文件夹的绝对路径 cat input.txt 查看input.txt文件的内容 ls 显示当前文件夹下全部的文件及子文件夹 rm recommender-d ...

  7. 设计Mysql索引的原则

    1. 搜索的索引列,不一定是所要选择的列.换句话说,最适合索引的列是出如今WHERE 子句中的列,或连接子句中指定的列,而不是出如今SELECT keyword后的选择列表中的列. 2. 使用惟一索引 ...

  8. android studio 怎样正确导入jar

    近期又開始做android,使用android studio中遇到导入jar没有反应的问题,查了下资料实践攻克了,现特地写一下博客.希望对刚刚的使用的android studio的朋友有帮助. 1.先 ...

  9. 猫学习IOS(五岁以下儿童)UI之360其他下载管理器广场UI

    猫分享.必须精品 下载材料:http://blog.csdn.net/u013357243/article/details/44486651 先看效果 主要是完毕了九宫格UI的搭建 代码 - (voi ...

  10. 每天进步一点点之SQL 获取表中某个时间字段离当前时间最近的几条

    实际中用到的SQL: select * from (select top 3 Id, case when startSignup>GETDATE() then '敬请期待' when (star ...