# How to get only the duplicatet items from an array

**URL:** <https://rubytalk.org/t/how-to-get-only-the-duplicatet-items-from-an-array/32422>\
**Category:** ruby-talk\
**Created:** [27 October 2006 17:54 UTC](https://rubytalk.org/t/how-to-get-only-the-duplicatet-items-from-an-array/32422 "2006-10-27T17:54:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@Gavin\_Kistner](https://rubytalk.org/u/Gavin_Kistner)\
**Post date:** [27 October 2006 17:54 UTC](https://rubytalk.org/t/how-to-get-only-the-duplicatet-items-from-an-array/32422/1 "2006-10-27T17:54:36Z")

</div>

> is there a oneline to get only the duplicatet item from an array?
> 
> i have an array like this  
> aTemp = [1, 1, 2, 2, 3, 4]  
> and i want to know, which items are duplicatet.

There are faster ways to do this, but since you asked for a one-line:  
a = [1, 1, 2, 2, 3, 4]  
duplicates = a.delete\_if{ |x| a.grep(x).length == 1 }.uniq  
#=\> [1, 2]

> **···**
>
> From: Nico Landgraf [mailto:nico@nico-landgraf.de]

---

<div class="post-metadata">

**Author:** ![Nico\_Landgraf](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Nico\_Landgraf](https://rubytalk.org/u/Nico_Landgraf)\
**Post date:** [27 October 2006 18:00 UTC](https://rubytalk.org/t/how-to-get-only-the-duplicatet-items-from-an-array/32422/2 "2006-10-27T18:00:17Z")

</div>

thanks, yes sir,  
thats really what i'm looking for.  
could you please post your fast way.

nico

Gavin Kistner wrote:

> **···**
>
> > From: Nico Landgraf [mailto:nico@nico-landgraf.de]
> > 
> > > is there a oneline to get only the duplicatet item from an array?
> > > 
> > > i have an array like this  
> > > aTemp = [1, 1, 2, 2, 3, 4]  
> > > and i want to know, which items are duplicatet.
> > 
> > There are faster ways to do this, but since you asked for a one-line:  
> > a = [1, 1, 2, 2, 3, 4]  
> > duplicates = a.delete\_if{ |x| a.grep(x).length == 1 }.uniq  
> > #=\> [1, 2]

---

<div class="post-metadata">

**Author:** ![Brad5](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Brad5](https://rubytalk.org/u/Brad5)\
**Post date:** [27 October 2006 19:12 UTC](https://rubytalk.org/t/how-to-get-only-the-duplicatet-items-from-an-array/32422/3 "2006-10-27T19:12:09Z")

</div>

Nico Landgraf wrote:

> thanks, yes sir,  
> thats really what i'm looking for.  
> could you please post your fast way.

Use Set:

irb(main):006:0\> a = [1,1,2,2,3,3,4,4]  
=\> [1, 1, 2, 2, 3, 3, 4, 4]  
irb(main):007:0\> Set.new(a)  
=\> #\<Set: {1, 2, 3, 4}\>

I don't know if it's faster, but it look better IMO.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![William\_Fisk](https://avatars.discourse-cdn.com/v4/letter/w/ec9cab/32.png) [@William\_Fisk](https://rubytalk.org/u/William_Fisk)\
**Post date:** [28 October 2006 18:54 UTC](https://rubytalk.org/t/how-to-get-only-the-duplicatet-items-from-an-array/32422/4 "2006-10-28T18:54:52Z")

</div>

another possibility:

> > a=['a','b',7,77,'a','ba',3,7]

=\> ["a", "b", 7, 77, "a", "ba", 3, 7]

> > a.inject(Hash.new(0)){|h,v|h[v]+=1;h}.reject{|k,v| v\<2}.keys

=\> ["a", 7]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
