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. C语言程序代写(Linux下线程)

    联系QQ:928900200 CSCI 3120 Operating Systems Summer 2014 Handout 3Assignment 2Date Due: June 5, 2014 b ...

  2. 【原创】leetCodeOj --- Excel Sheet Column Title 解题报告

    题目地址: https://oj.leetcode.com/problems/excel-sheet-column-title/ 题目内容: Given a positive integer, ret ...

  3. Light OJ Dynamic Programming

    免费做一样新 1004 - Monkey Banana Problem 号码塔 1005 - Rooks 排列 1013 - Love Calculator LCS变形 dp[i][j][k]对于第一 ...

  4. MyReport报表引擎2.1.0.0新功能

    支持简单的四则运算样例1:算总分,简单连加 样例2:算平均分.除非和加法混合(可以识别先乘除后加减,不支持小括号优先运算) 新增行号函数 直接生成行号 新增多种格式化设置修复数个已发现的Bug.优化代 ...

  5. Linux下一个OTL 采用long long类型数据库支持BIGINT

    码如下面: #define OTL_BIGINT long long #define OTL_STR_TO_BIGINT(str,n) \ { \ n=atoll(str); \ } #define ...

  6. Ognl底层使用

    今天,在得知ognl采用.在这里和大家分享一下.我希望能帮助. package com.hcj.test; import java.util.ArrayList; import java.util.L ...

  7. 水题 hdu1002------用BigInteger解决大数问题

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  8. Mybatis简单的入门之增删改查

    一般的过程例如以下 1.加入Mybatis所须要的包,和连接数据库所需的包 2.配置mybatis-config.xml文件 3.配置与pojo相应的映射文件 mybatis-config,xml & ...

  9. Cocos2d-x学习笔记(9)(CCTextFieldTTF使用输入框)

    1.CCTextFieldTTF创建和使用 CCTextFieldTTF::create(const char* placeholder,const char* fontName.float font ...

  10. java设计模式演示示例

    创建一个模式 1.工厂方法模式(Factory Method)  该程序创建的操作对象,独自一人走出流程,创建产品工厂接口.实际的工作转移到详细的子类.大大提高了系统扩展的柔性,接口的抽象化处理给相互 ...