141

What's the most elegant way to select out objects in an array that are unique with respect to one or more attributes?

These objects are stored in ActiveRecord so using AR's methods would be fine too.

Laurie Young
  • 136,234
  • 13
  • 47
  • 54
sutee
  • 12,568
  • 13
  • 49
  • 61

15 Answers15

224

Use Array#uniq with a block:

@photos = @photos.uniq { |p| p.album_id }
Lane
  • 4,682
  • 1
  • 36
  • 20
22

Add the uniq_by method to Array in your project. It works by analogy with sort_by. So uniq_by is to uniq as sort_by is to sort. Usage:

uniq_array = my_array.uniq_by {|obj| obj.id}

The implementation:

class Array
  def uniq_by(&blk)
    transforms = []
    self.select do |el|
      should_keep = !transforms.include?(t=blk[el])
      transforms << t
      should_keep
    end
  end
end

Note that it returns a new array rather than modifying your current one in place. We haven't written a uniq_by! method but it should be easy enough if you wanted to.

EDIT: Tribalvibes points out that that implementation is O(n^2). Better would be something like (untested)...

class Array
  def uniq_by(&blk)
    transforms = {}
    select do |el|
      t = blk[el]
      should_keep = !transforms[t]
      transforms[t] = true
      should_keep
    end
  end
end
Daniel Lucraft
  • 7,196
  • 6
  • 34
  • 35
  • 1
    Nice api but that's going to have poor (looks like O(n^2)) scaling performance for large arrays. Could be fixed by making transforms a hashset. – tribalvibes Oct 27 '10 at 06:56
  • 7
    This answer is out of date. Ruby >= 1.9 has Array#uniq with a block that does exactly this, as in the accepted answer. – Peter H. Boling Dec 24 '14 at 01:48
17

Do it on the database level:

YourModel.find(:all, :group => "status")
mislav
  • 14,919
  • 8
  • 47
  • 63
16

You can use this trick to select unique by several attributes elements from array:

@photos = @photos.uniq { |p| [p.album_id, p.author_id] }
yauhenininjia
  • 198
  • 1
  • 7
6

I had originally suggested using the select method on Array. To wit:

[1, 2, 3, 4, 5, 6, 7].select{|e| e%2 == 0} gives us [2,4,6] back.

But if you want the first such object, use detect.

[1, 2, 3, 4, 5, 6, 7].detect{|e| e>3} gives us 4.

I'm not sure what you're going for here, though.

Alex M
  • 2,458
  • 20
  • 15
5

I like jmah's use of a Hash to enforce uniqueness. Here's a couple more ways to skin that cat:

objs.inject({}) {|h,e| h[e.attr]=e; h}.values

That's a nice 1-liner, but I suspect this might be a little faster:

h = {}
objs.each {|e| h[e.attr]=e}
h.values
Head
  • 4,691
  • 3
  • 30
  • 18
5

Use Array#uniq with a block:

objects.uniq {|obj| obj.attribute}

Or a more concise approach:

objects.uniq(&:attribute)
7mode
  • 86
  • 1
  • 6
4

The most elegant way I have found is a spin-off using Array#uniq with a block

enumerable_collection.uniq(&:property)

…it reads better too!

Igbanam
  • 5,904
  • 5
  • 44
  • 68
3

If I understand your question correctly, I've tackled this problem using the quasi-hacky approach of comparing the Marshaled objects to determine if any attributes vary. The inject at the end of the following code would be an example:

class Foo
  attr_accessor :foo, :bar, :baz

  def initialize(foo,bar,baz)
    @foo = foo
    @bar = bar
    @baz = baz
  end
end

objs = [Foo.new(1,2,3),Foo.new(1,2,3),Foo.new(2,3,4)]

# find objects that are uniq with respect to attributes
objs.inject([]) do |uniqs,obj|
  if uniqs.all? { |e| Marshal.dump(e) != Marshal.dump(obj) }
    uniqs << obj
  end
  uniqs
end
Drew Olson
  • 3,709
  • 3
  • 23
  • 14
2

You can use a hash, which contains only one value for each key:

Hash[*recs.map{|ar| [ar[attr],ar]}.flatten].values
jmah
  • 2,216
  • 14
  • 16
2

Rails also has a #uniq_by method.

Reference: Parameterized Array#uniq (i.e., uniq_by)

double-beep
  • 5,031
  • 17
  • 33
  • 41
apb
  • 3,270
  • 3
  • 29
  • 23
1

ActiveSupport implementation:

def uniq_by
  hash, array = {}, []
  each { |i| hash[yield(i)] ||= (array << i) }
  array
end
grosser
  • 14,707
  • 7
  • 57
  • 61
1

I like jmah and Head's answers. But do they preserve array order? They might in later versions of ruby since there have been some hash insertion-order-preserving requirements written into the language specification, but here's a similar solution that I like to use that preserves order regardless.

h = Set.new
objs.select{|el| h.add?(el.attr)}
TKH
  • 828
  • 6
  • 11
0

Now if you can sort on the attribute values this can be done:

class A
  attr_accessor :val
  def initialize(v); self.val = v; end
end

objs = [1,2,6,3,7,7,8,2,8].map{|i| A.new(i)}

objs.sort_by{|a| a.val}.inject([]) do |uniqs, a|
  uniqs << a if uniqs.empty? || a.val != uniqs.last.val
  uniqs
end

That's for a 1-attribute unique, but the same thing can be done w/ lexicographical sort ...

Purfideas
  • 3,288
  • 1
  • 24
  • 17
0

If you are not married with arrays, we can also try eliminating duplicates through sets

set = Set.new
set << obj1
set << obj2
set.inspect

Note that in case of custom objects, we need to override eql? and hash methods