matching arrays within arrays

N

Nick Black

Hello,

I have a number of arrays within arrays. Some of them are duplicatess like:

[[foo, bar, ru, by], [ru], [bar, foo], [foo, bar, ru, by]]

I would like to remove duplicates to end up with:

[[foo, bar, ru, by], [ru], [bar, foo]]

Is there a Ruby function to do this?


Cheers,
 
A

Alex Young

Nick said:
Hello,

I have a number of arrays within arrays. Some of them are duplicatess
like:

[[foo, bar, ru, by], [ru], [bar, foo], [foo, bar, ru, by]]

I would like to remove duplicates to end up with:

[[foo, bar, ru, by], [ru], [bar, foo]]

Is there a Ruby function to do this?

irb(main):007:0> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'],
['foo', 'bar', 'ru', 'by']].uniq
=> [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]
 
C

Chris Gernon

Nick said:
Is there a Ruby function to do this?

uniq will work. It doesn't matter what type of object the members of the
array are, so it can be used for an array of arrays.

irb> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'], ['foo', 'bar',
'ru', 'by']].uniq
=> [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]
 
N

Nick Black

Cheers!

Nick said:
Is there a Ruby function to do this?

uniq will work. It doesn't matter what type of object the members of the
array are, so it can be used for an array of arrays.

irb> [['foo', 'bar', 'ru', 'by'], ['ru'], ['bar', 'foo'], ['foo', 'bar',
'ru', 'by']].uniq
=> [["foo", "bar", "ru", "by"], ["ru"], ["bar", "foo"]]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top