Ruby: Count unique elements and their occurences in an array
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)counts => {"A"=>4, "B"=>2, "C"=>1}
['A','A','A','A','B','B','C'].each { |name| counts[name] += 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_hOr 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的更多相关文章
- [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 ...
- FB面经 Prepare: Count Unique Island
数unique island, 比如 110000 110001 001101 101100 100000 总共两个unique岛,不是四个 方法可以是记录每次新的岛屿搜索的路径,left,right ...
- 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) 空间复杂度 ...
- [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 ...
- numpy.unique
Find the unique elements of an array. Returns the sorted unique elements of an array. There are thre ...
- [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 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 ...
- 【ruby】ruby基础知识
Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...
- Non-unique Elements
Non-unique Elements You are given a non-empty list of integers (X). For this task, you should return ...
随机推荐
- SQLServer 网络协议(一)
SQLserver现在主要的3种协议:Shared Memory.TCP/IP 和 Named Pipe SharedMemory: Shared Memory最快最简单的协议,使用SharedMem ...
- tokumx经营报表
#见数据库列表 show dbs #切换/创建数据库(当创建一个集合(table)的时候会自己主动创建当前数据库) use admin; #添加用户 db.addUser("zhoulf ...
- 聊聊并发(六)——ConcurrentLinkedQueue的实现原理分析
1. 引言 在并发编程中我们有时候需要使用线程安全的队列.如果我们要实现一个线程安全的队列有两种实现方式:一种是使用阻塞算法,另一种是使用非阻塞算法.使用阻塞算法的队列可以用一个锁(入队和出队用同一把 ...
- 【原创】leetCodeOj --- Merge k Sorted Lists 解题报告
题目地址: https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题目内容: /** * Definition for singly-linke ...
- Android AlarmManager报警的实现
什么是AlarmManager? AlarmManager它是Android经常使用的系统-Level提醒服务,我们指定为广播中的特定时间Intent. 我们设定一个时间,然后在该时间到来时.Alar ...
- 下的生产环境was重新启动不同意,怎么做?
前一段时间上线.遇到一个jndi问题,它是如何是个问题?它是在测试环境中的原始没有问题,在生产环境中,您无法连接生产数据库,然后发现问题,那是,ibm工具生成在测试环境中自己主动的连接jndi资源文件 ...
- iOS 真机调试(最具体的步骤来解决历史,hmt精心打造)
/*************************************************************1************************************* ...
- atitit.设计模式(2) -----查询方式/ command 总结
atitit.设计模式(2) -----查询方式/ command 总结 1. 应用场景: 1 1. 代替一瓦if else 1 2. 建设api rpc风格的时候儿. 1 3. 菜单是Command ...
- BootStrap布局案例
BootStrap布局 bootstrap 2.3版与3.0版的使用区别 http://www.weste.net/2013/8-20/93261.html 以一个博客系统的首页,来介绍如何布局 1, ...
- 【Linux探索之旅】第一部分第二课:下载Linux,免费的噢
内容简介 1.第一部分第二课:下载Linux,免费的噢 2.第一部分第三课预告:测试并安装Ubuntu 下载Linux,免费的噢 大家好,上一课我们认识了非常“霸气侧漏”的Linux操作系统. 也知道 ...