110

I have a Ruby class

class MyClass
  attr_writer :item1, :item2
end

my_array = get_array_of_my_class() #my_array is an array of MyClass
unique_array_of_item1 = []

I want to push MyClass#item1 to unique_array_of_item1, but only if unique_array_of_item1 doesn't contain that item1 yet. There is a simple solution I know: just iterate through my_array and check if unique_array_of_item1 already contains the current item1 or not.

Is there any more efficient solution?

iltempo
  • 15,718
  • 8
  • 61
  • 72
Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

6 Answers6

161

@Coorasse has a good answer, though it should be:

my_array | [item]

And to update my_array in place:

my_array |= [item]
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Jason Denney
  • 3,191
  • 1
  • 18
  • 14
90

You can use Set instead of Array.

fabdurso
  • 2,366
  • 5
  • 29
  • 55
Jiří Pospíšil
  • 14,296
  • 2
  • 41
  • 52
  • While it is true that the docs say that Sets are not order, they are in fact (as of Ruby 1.9) ordered. If you look at the code, the main methods you'd use to get at the order (such as `Set#each` and `Set#to_a`) delegate to `@hash`. And as of Ruby 1.9 Hashes are ordered. "Hashes enumerate their values in the order that the corresponding keys were inserted." http://ruby-doc.org/core-1.9.1/Hash.html – phylae May 05 '15 at 04:47
  • Never new there was such a thing as a set. They are awesome, thank you so much – Brad Jun 09 '17 at 11:08
42

You don't need to iterate through my_array by hand.

my_array.push(item1) unless my_array.include?(item1)

Edit:

As Tombart points out in his comment, using Array#include? is not very efficient. I'd say the performance impact is negligible for small Arrays, but you might want to go with Set for bigger ones.

doesterr
  • 3,955
  • 19
  • 26
  • 8
    you definitely don't wanna do that! `array.include?(item)` has complexity `O(n)` - so it's like iterating the whole array. have a look at this benchmark: https://gist.github.com/deric/4953652 – Tombart Feb 14 '13 at 15:49
  • Beautiful solution :). Readable! In my case, I cannot have a set, so I'm using this solution. – Victor Nov 09 '20 at 07:56
  • You can also use binary search to increase the performance if the array is ordered. `[1, 2, 3, 4, 5].bsearch { |e| e == 3 }` – Victor Nov 09 '20 at 08:03
35

You can convert item1 to array and join them:

my_array | [item1]
coorasse
  • 5,278
  • 1
  • 34
  • 45
4

Important to keep in mind that the Set class and the | method (also called "Set Union") will yield an array of unique elements, which is great if you want no duplicates but which will be an unpleasant surprise if you have non-unique elements in your original array by design.

If you have at least one duplicate element in your original array that you don't want to lose, iterating through the array with an early return is worst-case O(n), which isn't too bad in the grand scheme of things.

class Array
  def add_if_unique element
    return self if include? element
    push element
  end
end
elreimundo
  • 6,186
  • 1
  • 13
  • 6
0

I'm not sure if it's perfect solution, but worked for me:

    host_group = Array.new if not host_group.kind_of?(Array)
    host_group.push(host)
witkacy26
  • 91
  • 6
  • This did not work for you, unless uniqueness isn't a constraint. It does not *uniquely* add elements to the array. >> a = [1,2,3]; => [1, 2, 3] >> a.push(3); => [1, 2, 3, 3] >> a.push(3); => [1, 2, 3, 3, 3] >> a.push(3); => [1, 2, 3, 3, 3, 3] – Peter H. Boling Oct 01 '21 at 23:42