[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Returning a duplicate from an Array

Andy Cooper

1/13/2009 10:05:00 PM

> Hey guys,
> I have an array of names that may contain duplicates. What I=20
> would like
> to do is return an array OF THE DUPLICATES. I was looking around at
> various built-in methods for arrays, but the closest thing I=20
> can find is
> Array.uniq, which returns the array without the duplicates.
>=20
> Any suggestions on how to do this?
>=20
> Thanks,
> - Jeff Miller
> --=20

Without really looking into it a quick and dirty way would be to just
subtract the results of .uniq from the initial array.

Andy Cooper.

1 Answer

Jeff Miller

1/13/2009 10:18:00 PM

0

Andy Cooper wrote:
>
> Without really looking into it a quick and dirty way would be to just
> subtract the results of .uniq from the initial array.
>
> Andy Cooper.

Thanks for the quick reply! However, after trying it, I found that
subtracting an array from an array will delete each instance of an
element, leaving me with a blank array, IE:

[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]

Any other ideas?

Thanks again,
- Jeff Miller
--
Posted via http://www.ruby-....