dax
dax

Reputation: 10997

More efficient way to collect unique attributes from an array of arrays

given a data structure as follows:

[ 
  [
    "1", 
    {
      :user_uid=>"1", 
      :display_name=>"joe", 
      :country_code=>"ITA", 
      # more attributes
    }
  ],
  # more users
]

I can get a unique list of all the country_code attributes like this:

users.map do |uid, attributes|
  attributes[:country_code]
end.uniq

but if I have a very large dataset, this still loops through every user (As Ajedi32 pointed out, of course that's the case).

Is there a more efficient way to collect this data?

Upvotes: 1

Views: 77

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110675

I would suggest:

 require 'set`

 users.each_with_object(Set.new) { |(_, attributes),s|
     s <<  attributes[:country_code]  }.to_a

Upvotes: 1

SHS
SHS

Reputation: 7744

With 1 user, I found the following to be faster than uniq when I compared the time it took each method to run 500 times. Could help you.

begin
  array = []

  users.each do |_, attributes|
    array |= [attributes[:country_code]]
    # http://www.ruby-doc.org/core-2.2.0/Array.html#method-i-7C
  end

  array
end

Upvotes: 1

Related Questions