How to Get the Unique Elements from an Array of Hashes in Ruby

Unique on an array of hashes based on value

In Ruby 1.9, try the following

a.uniq! {|e| e[:color] }

Ruby -- array of hashes, getting unique values

You can combine map and uniq:

persons = [
{name: 'Mark', age: 28},
{name: 'John', age: 45},
{name: 'Sam', age: 34},
{name: 'John', age: 34}
]

persons.map { |p| p[:name] }.uniq #=> ["Mark", "John", "Sam"]

Unique array of hashes by key value

Try this by using Array#uniq:

arr.uniq{|h| h[:text]} # Returns a new array by removing duplicate values
=> [{:id=>0, :text=>"someText"}, {:id=>1, :text=>"anotherText"}]
# OR
arr.uniq!{|h| h[:text]} # Removes duplicate elements from self.
=> [{:id=>0, :text=>"someText"}, {:id=>1, :text=>"anotherText"}]

There will be many different approaches to achieve your goal but as you are looking for fastest way, Here is benchmark of both uniq and group_by. This is just for sample. Like this you can test yourself different approaches and check for the solution as per your requirements..

require 'benchmark'
arr = [{ :id => 0, :text => "someText" }, { :id => 1, :text => "anotherText" }, { :id => 2, :text => "someText" }]
Benchmark.bm do |x|
x.report("group_by:") { arr.group_by { |e| e[:text] }.values.map &:first }
x.report("uniq:") { arr.uniq{|h| h[:text]} }
end
# output
user system total real
group_by: 0.000000 0.000000 0.000000 ( 0.000039)
uniq: 0.000000 0.000000 0.000000 ( 0.000012)

How do I get the unique elements from an array of hashes in Ruby?

I can get what I want by calling inject

a = [{:a => 1},{:a => 2}, {:a => 1}]
a.inject([]) { |result,h| result << h unless result.include?(h); result }

This will return:

[{:a=>1}, {:a=>2}]

How do I get the unique elements and count from an array of hashes in Ruby?

Try this:

a = [{:key => 2}, {:key => 1}, {:key => 4}, {:key => 1}]
b = a.clone
a.uniq!
a.inject([]) { |result,h| h[:count]=b.count(h) if b.count(h) > 1 ; result << h; result }

=> [{:key=>2}, {:key=>1, :count=>2}, {:key=>4}]

Ruby - extracting the unique values per key from an array of hashes

Use Array#uniq:

array_of_hashes = [ {'a' => 1, 'b' => 2 , 'c' => 3} , 
{'a' => 4, 'b' => 5 , 'c' => 3},
{'a' => 6, 'b' => 5 , 'c' => 3} ]

array_of_hashes.map { |h| h['a'] }.uniq # => [1, 4, 6]
array_of_hashes.map { |h| h['b'] }.uniq # => [2, 5]
array_of_hashes.map { |h| h['c'] }.uniq # => [3]

Get unique properties from array of hashes in ruby

Your current approach is already pretty good; I don't see much room for improvement. I would write it like this:

def my_fantastic_method(data_list)
data_list.each_with_object(Hash.new { |h, k| h[k] = Set.new }) do |data, result|
data.attributes.each do |key, value|
result[key.to_sym] << value
end
end
end
  • By setting a default value on each hash value, I have eliminated the need to explicitly declare foo: [], bar: [].
  • By using each_with_object, I have eliminated the need to declare a local variable and explicitly return it at the end.
  • By using Set, there is no need to call uniq on the final result. This requires less code, and is more performant. However, if you really want the final result to be a mapping to Arrays rather than Sets, then you would need to call to_a on each value at the end of the method.
  • I have used different variable names for data_list and data. Call these whatever you like, but it's typically considered bad practice to shadow outer variables.

How do I search within an array of hashes by hash values in ruby?

You're looking for Enumerable#select (also called find_all):

@fathers.select {|father| father["age"] > 35 }
# => [ { "age" => 40, "father" => "Bob" },
# { "age" => 50, "father" => "Batman" } ]

Per the documentation, it "returns an array containing all elements of [the enumerable, in this case @fathers] for which block is not false."

get unique key values from an array of array of hash in ruby to perform grouping

Does this would do?

arr.flatten.group_by {|elem| elem[:focus]}

If you just need the uniq focus values,

arr.flatten.map {|elem| elem[:focus] }.uniq


Related Topics



Leave a reply



Submit