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)
[‘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}) }

时间: 2024-10-13 21:38:25

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

Count the number of occurrences in a sorted array

Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output: 4 // x (or 2) occurs 4 times in arr[] Input: arr

Count frequencies of all elements in array in O(1) extra space and O(n) time

Given an unsorted array of n integers which can contain integers from 1 to n. Some elements can be repeated multiple times and some other elements can be absent from the array. Count frequency of all elements that are present and print the missing el

【ruby】ruby基础知识

Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for Linux tryhttp://www.rpmfind.net. Our first program(从此开始) Enter the following into the file, "test.rb". ? 1 puts "Howdy!" At the C: promp

*Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet

Non-unique Elements

Non-unique Elements You are given a non-empty list of integers (X). For this task, you should return a list consisting of only the non-unique elements in this list. To do so you will need to remove all unique elements (elements which are contained in

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,2,3] and k = 2, return [1,2]. Note: 347. Top K Frequent ElementsYou may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's

[LeetCode]Top K Frequent Elements

题目描述: Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must

347. Top K Frequent Elements [medium] (Python)

题目链接 https://leetcode.com/problems/top-k-frequent-elements/ 题目原文 Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ numbe

[LeetCode] 347. Top K Frequent Elements 解题思路 - Java

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet