378

I have a Ruby array which contains duplicate elements.

array = [1,2,2,1,4,4,5,6,7,8,5,6]

How can I remove all the duplicate elements from this array while retaining all unique elements without using for-loops and iteration?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52

8 Answers8

807
array = array.uniq

uniq removes all duplicate elements and retains all unique elements in the array.

This is one of many beauties of the Ruby language.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
  • 55
    no, the uniq! method will return nil if the array had been unique yet Ex: a = [1,2,3,4] a.uniq -> [1,2,3,4] but a.uniq! -> nil – duykhoa Apr 04 '13 at 08:37
  • 15
    i wouldn't really see this as a beauty of the ruby language...it's just the beauty of the ruby standard library? don't get me wrong, there are a lot of beautiful things about the language. – Justin L. May 20 '13 at 08:58
  • 8
    write the same in Objective-C, Javascript and PHP. Then tell us that Ruby isn't a beautiful language! – Adam Waite Jun 26 '13 at 20:41
  • @AdamWaite For example in php you cant make this in easy way: "If a block is given, it will use the return value of the block for comparison." And ruby syntax of array.uniq is more short, clear and coincise than php -> array_unique(array). http://ruby-doc.org/core-2.0/Array.html#method-i-uniq – Simonini Aug 14 '13 at 08:18
  • 1
    @AdamWaite: yes ruby is; but its again "uniq" not "unique" :'|, I always/still fight with so little things with ruby – halfbit Jul 10 '14 at 22:14
  • 4
    This also works for complex types: `[{how: "are"}, {u:"doing"}, {how: "are"}].uniq => [{:how=>"are"}, {:u=>"doing"}]` – Blaskovicz Jan 30 '16 at 06:29
  • Also, `.uniq!` is a mutator. In case you wanted to preserve your `array` – taystack May 12 '16 at 22:52
  • 5
    about what @duykhoa says, the uniq! method returns nil, but you usually don't care about the return of a `.uniq!` it does the work on the object itself – carpinchosaurio Oct 23 '16 at 00:43
  • it should also be mentioned that a block can be given if the uniqueness constraint is more complex; for instance, you could do `array.uniq { |i| i % 3 }` to get the unique values modulo 3 (1, 2, 6) – Ja͢ck Aug 17 '22 at 07:52
90

You can return the intersection.

a = [1,1,2,3]
a & a

This will also delete duplicates.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jaredsmith
  • 7,306
  • 3
  • 20
  • 31
  • 16
    Functionally, this answer is correct, but I think this is markedly less readable than just using uniq. – Fiona T Apr 19 '13 at 22:57
  • 23
    I was just putting it here so whoever visits this page will see other ways of doing it as well, I was not trying to say it is better in any way. – jaredsmith Apr 23 '13 at 20:08
  • 5
    The reason this works is because when using set operations, the resulting array is treated as a set, which is a data structure that usually has no repeat values. Using `a | a` (union) would do the same trick. – Cezar Aug 11 '13 at 03:58
59

You can remove the duplicate elements with the uniq method:

array.uniq  # => [1, 2, 4, 5, 6, 7, 8]

What might also be useful to know is that uniq takes a block, so if you have a have an array of keys:

["bucket1:file1", "bucket2:file1", "bucket3:file2", "bucket4:file2"]

and you want to know what the unique files are, you can find it out with:

a.uniq { |f| f[/\d+$/] }.map { |p| p.split(':').last }
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Marek Příhoda
  • 11,108
  • 3
  • 39
  • 53
  • 5
    I'm a bit confused by this. The block is used if you need your own comparison function -- in your example, sending `uniq` to that array without a block would return the same value as it does with your block. – hdgarrood Jan 28 '13 at 11:35
22

If someone was looking for a way to remove all instances of repeated values, see "How can I efficiently extract repeated elements in a Ruby array?".

a = [1, 2, 2, 3]
counts = Hash.new(0)
a.each { |v| counts[v] += 1 }
p counts.select { |v, count| count == 1 }.keys # [1, 3]
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Lri
  • 26,768
  • 8
  • 84
  • 82
20

Just another alternative if anyone cares.

You can also use the to_set method of an array which converts the Array into a Set and by definition, set elements are unique.

[1,2,3,4,5,5,5,6].to_set => [1,2,3,4,5,6]
Finks
  • 1,661
  • 2
  • 16
  • 25
  • 5
    If you care about memory, `to_set` will allocate 4 objects, while `uniq` allocates one. – Jan Klimo Jul 02 '19 at 05:48
  • Yeah, I set .uniq vs .to_set in a benchmark test and got 32 ms for .to_set and 7 ms for .uniq. In conclusion, it's way less programmatically expensive to use .uniq – iarobinson Jun 13 '22 at 16:02
12

The simplest ways for me are these ones:

array = [1, 2, 2, 3]

Array#to_set

array.to_set.to_a

# [1, 2, 3]

Array#uniq

array.uniq

# [1, 2, 3]
Victor
  • 1,904
  • 18
  • 18
3

Just to provide some insight:

require 'fruity'
require 'set'

array = [1,2,2,1,4,4,5,6,7,8,5,6] * 1_000

def mithun_sasidharan(ary)
  ary.uniq
end

def jaredsmith(ary)
  ary & ary
end

def lri(ary)
  counts = Hash.new(0)
  ary.each { |v| counts[v] += 1 }
  counts.select { |v, count| count == 1 }.keys 
end

def finks(ary)
  ary.to_set
end

def santosh_mohanty(ary)
    result = ary.reject.with_index do |ele,index|
      res = (ary[index+1] ^ ele)
      res == 0
    end
end

SHORT_ARRAY = [1,1,2,2,3,1]
mithun_sasidharan(SHORT_ARRAY) # => [1, 2, 3]
jaredsmith(SHORT_ARRAY) # => [1, 2, 3]
lri(SHORT_ARRAY) # => [3]
finks(SHORT_ARRAY) # => #<Set: {1, 2, 3}>
santosh_mohanty(SHORT_ARRAY) # => [1, 2, 3, 1]

puts 'Ruby v%s' % RUBY_VERSION

compare do
  _mithun_sasidharan { mithun_sasidharan(array) }
  _jaredsmith { jaredsmith(array) }
  _lri { lri(array) }
  _finks { finks(array) }
  _santosh_mohanty { santosh_mohanty(array) }
end

Which, when run, results in:

# >> Ruby v2.7.1
# >> Running each test 16 times. Test will take about 2 seconds.
# >> _mithun_sasidharan is faster than _jaredsmith by 2x ± 0.1
# >> _jaredsmith is faster than _santosh_mohanty by 4x ± 0.1 (results differ: [1, 2, 4, 5, 6, 7, 8] vs [1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, ...
# >> _santosh_mohanty is similar to _lri (results differ: [1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, 7, 8, 5, 6, 1, 2, 1, 4, 5, 6, ...
# >> _lri is similar to _finks (results differ: [] vs #<Set: {1, 2, 4, 5, 6, 7, 8}>)

Note: these returned bad results:

  • lri(SHORT_ARRAY) # => [3]
  • finks(SHORT_ARRAY) # => #<Set: {1, 2, 3}>
  • santosh_mohanty(SHORT_ARRAY) # => [1, 2, 3, 1]
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
-4

Try using the XOR operator, without using built-in functions:

a = [3,2,3,2,3,5,6,7].sort!

result = a.reject.with_index do |ele,index|
  res = (a[index+1] ^ ele)
  res == 0
end

print result

With built-in functions:

a = [3,2,3,2,3,5,6,7]

a.uniq
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Santosh Mohanty
  • 468
  • 1
  • 6
  • 23
  • 3
    I haven't downvoted and I know next to nothing about Ruby, but isn't `.sort!` also an inbuilt function? – Carolus Mar 12 '20 at 12:10