# Duplicate elements in array

**URL:** <https://rubytalk.org/t/duplicate-elements-in-array/41728>\
**Category:** ruby-talk\
**Created:** [28 October 2007 12:47 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728 "2007-10-28T12:47:39Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shuaib\_Zahda](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@Shuaib\_Zahda](https://rubytalk.org/u/Shuaib_Zahda)\
**Post date:** [28 October 2007 12:47 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/1 "2007-10-28T12:47:39Z")

</div>

Hello

I am trying to output the duplicate elements in an array. I looked into  
the api of ruby I found uniq method which outputs the array with no  
duplication. What i want is to know which elements is duplicated.  
For example

array = ["apple", "banana", "apple", "orange"]  
=\> ["apple", "banana", "apple", "orange"]  
array.uniq  
=\> ["apple", "banana", "orange"]

I want the method to tell me that apple is the duplicated element

I tried this but it does not work

array - array.uniq

any idea

Regards  
Shuaib

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

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [28 October 2007 13:12 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/2 "2007-10-28T13:12:18Z")

</div>

think of it right now):

array = ["apple", "banana", "apple", "orange"]  
counts = array.inject(Hash.new {|h,k| h[k] = 0 }) { |hash, item| hash[item]  
+= 1; hash}  
p counts #=\> {"apple"=\>2, "banana"=\>1, "orange"=\>1}  
p counts.select { |k,v| v \> 1 }.map{ |k, v| k}.flatten #=\> ["apple"]

Regards,  
Sean

> **···**
>
> On 10/28/07, Shuaib Zahda \<shuaib.zahda@gmail.com\> wrote:
> 
> > Hello
> > 
> > I am trying to output the duplicate elements in an array. I looked into  
> > the api of ruby I found uniq method which outputs the array with no  
> > duplication. What i want is to know which elements is duplicated.  
> > For example
> > 
> > array = ["apple", "banana", "apple", "orange"]  
> > =\> ["apple", "banana", "apple", "orange"]  
> > array.uniq  
> > =\> ["apple", "banana", "orange"]
> > 
> > I want the method to tell me that apple is the duplicated element
> > 
> > I tried this but it does not work
> > 
> > array - array.uniq
> > 
> > any idea
> > 
> > Regards  
> > Shuaib  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > 
> > Here's one way (I'm sure there must be a simpler approach - just can't

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [28 October 2007 13:15 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/3 "2007-10-28T13:15:40Z")

</div>

Shuaib Zahda wrote:

> Hello
> 
> I am trying to output the duplicate elements in an array. I looked into  
> the api of ruby I found uniq method which outputs the array with no  
> duplication. What i want is to know which elements is duplicated.  
> For example
> 
> array = ["apple", "banana", "apple", "orange"]  
> =\> ["apple", "banana", "apple", "orange"]  
> array.uniq  
> =\> ["apple", "banana", "orange"]
> 
> I want the method to tell me that apple is the duplicated element
> 
> I tried this but it does not work
> 
> array - array.uniq
> 
> any idea
> 
> Regards  
> Shuaib  
> &nbsp;&nbsp;  
> I don't know a good way to do it, but one way to get the result would be to force it into a hash since that eliminates duplicates.

I'm sure there's a better way to do it, but here's what I got.

array = ["apple", "banana", "apple", "orange", "fat", "cow", "cow"]  
h = Hash.new  
duplicates =

array.each {|item|  
&nbsp;&nbsp;&nbsp;&nbsp;if h.has\_key?(item) then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;duplicates \<\< item  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[item] = 0 #it doesn't matter what we store  
&nbsp;&nbsp;&nbsp;&nbsp;end  
}

puts duplicates

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![Harry3](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@Harry3](https://rubytalk.org/u/Harry3)\
**Post date:** [28 October 2007 13:55 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/4 "2007-10-28T13:55:07Z")

</div>

arr,dup = ["apple", "banana", "apple", "orange"],  
(arr.length-1).times do  
&nbsp;&nbsp;a = arr.shift  
&nbsp;&nbsp;dup \<\< a if arr.include?(a)  
end  
p dup.uniq

Harry

> **···**
>
> On 10/28/07, Shuaib Zahda \<shuaib.zahda@gmail.com\> wrote:
> 
> > Hello
> > 
> > I am trying to output the duplicate elements in an array. I looked into  
> > the api of ruby I found uniq method which outputs the array with no  
> > duplication. What i want is to know which elements is duplicated.  
> > For example
> > 
> > array = ["apple", "banana", "apple", "orange"]  
> > =\> ["apple", "banana", "apple", "orange"]  
> > array.uniq  
> > =\> ["apple", "banana", "orange"]
> > 
> > I want the method to tell me that apple is the duplicated element
> > 
> > I tried this but it does not work
> > 
> > array - array.uniq
> > 
> > any idea
> > 
> > Regards  
> > Shuaib  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> A Look into Japanese Ruby List in English
> 
> > **[Kakueki.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=kakueki.com)**
> >
> > Start using this domain right away. Straightforward domain shopping experience. Quick access to your domain.

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [28 October 2007 13:16 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/5 "2007-10-28T13:16:28Z")

</div>

Sean O'Halpin wrote:

> **···**
>
> > On 10/28/07, Shuaib Zahda \<shuaib.zahda@gmail.com\> wrote:  
> > &nbsp;&nbsp;
> > 
> > > Hello
> > > 
> > > I am trying to output the duplicate elements in an array. I looked into  
> > > the api of ruby I found uniq method which outputs the array with no  
> > > duplication. What i want is to know which elements is duplicated.  
> > > For example
> > > 
> > > array = ["apple", "banana", "apple", "orange"]  
> > > =\> ["apple", "banana", "apple", "orange"]  
> > > array.uniq  
> > > =\> ["apple", "banana", "orange"]
> > > 
> > > I want the method to tell me that apple is the duplicated element
> > > 
> > > I tried this but it does not work
> > > 
> > > array - array.uniq
> > > 
> > > any idea
> > > 
> > > Regards  
> > > Shuaib  
> > > --  
> > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > > 
> > > Here's one way (I'm sure there must be a simpler approach - just can't  
> > > &nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > think of it right now):
> > 
> > array = ["apple", "banana", "apple", "orange"]  
> > counts = array.inject(Hash.new {|h,k| h[k] = 0 }) { |hash, item| hash[item]  
> > += 1; hash}  
> > p counts #=\> {"apple"=\>2, "banana"=\>1, "orange"=\>1}  
> > p counts.select { |k,v| v \> 1 }.map{ |k, v| k}.flatten #=\> ["apple"]
> > 
> > Regards,  
> > Sean
> 
> I so have to get the hang of inject, flatten and map.
> 
> Cheers,  
> Mohit.  
> 10/28/2007 | 9:16 PM.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 October 2007 13:40 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/6 "2007-10-28T13:40:03Z")

</div>

irb(main):007:0\> array = %w{apple banana apple orange}  
=\> ["apple", "banana", "apple", "orange"]  
irb(main):008:0\> array.inject(Hash.new(0)) {|ha,e| ha[e]+=1;ha}.delete\_if {|k,v| v==1}.keys  
=\> ["apple"]

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 28.10.2007 14:16, Mohit Sindhwani wrote:
> 
> > Sean O'Halpin wrote:
> > 
> > > On 10/28/07, Shuaib Zahda \<shuaib.zahda@gmail.com\> wrote:
> > > 
> > > > Hello
> > > > 
> > > > I am trying to output the duplicate elements in an array. I looked into  
> > > > the api of ruby I found uniq method which outputs the array with no  
> > > > duplication. What i want is to know which elements is duplicated.  
> > > > For example
> > > > 
> > > > array = ["apple", "banana", "apple", "orange"]  
> > > > =\> ["apple", "banana", "apple", "orange"]  
> > > > array.uniq  
> > > > =\> ["apple", "banana", "orange"]
> > > > 
> > > > I want the method to tell me that apple is the duplicated element
> > > > 
> > > > I tried this but it does not work
> > > > 
> > > > array - array.uniq
> > > > 
> > > > any idea
> > > > 
> > > > Regards  
> > > > Shuaib  
> > > > --  
> > > > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> > > > 
> > > > Here's one way (I'm sure there must be a simpler approach - just can't  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;
> > > 
> > > think of it right now):
> > > 
> > > array = ["apple", "banana", "apple", "orange"]  
> > > counts = array.inject(Hash.new {|h,k| h[k] = 0 }) { |hash, item| hash[item]  
> > > += 1; hash}  
> > > p counts #=\> {"apple"=\>2, "banana"=\>1, "orange"=\>1}  
> > > p counts.select { |k,v| v \> 1 }.map{ |k, v| k}.flatten #=\> ["apple"]

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [28 October 2007 17:44 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/7 "2007-10-28T17:44:53Z")

</div>

Hi,

They are definitely worth looking into - inject in particular is a  
powerful tool (Robert Klemme can make it do anything!). However, the  
following benchmark shows that a slight modification of your approach  
is actually pretty efficient. (The modification is to store the  
duplicates in a hash rather than an array so you can return the list  
of duplicates using Hash#keys).

Regards,  
Sean

# Mohit Sindhwani (with slight adjustment)  
def duplicates\_1(array)  
&nbsp;&nbsp;seen = { }  
&nbsp;&nbsp;duplicates = { }  
&nbsp;&nbsp;array.each {|item| seen.key?(item) ? duplicates[item] = true :  
seen[item] = true}  
&nbsp;&nbsp;duplicates.keys  
end

# Robert Klemme  
def duplicates\_2(array)  
&nbsp;&nbsp;array.inject(Hash.new(0)) {|ha,e| ha[e]+=1;ha}.delete\_if {|k,v| v==1}.keys  
end

# from facets  
def duplicates\_3(array)  
&nbsp;&nbsp;array.inject(Hash.new(0)){|h,v| h[v]+=1; h}.reject{|k,v| v==1}.keys  
end

require 'benchmark'

def do\_benchmark(title, n, methods, \*args, &block)  
&nbsp;&nbsp;puts '-' \* 40  
&nbsp;&nbsp;puts title  
&nbsp;&nbsp;puts '-' \* 40  
&nbsp;&nbsp;Benchmark.bm(methods.map{ |x| x.to\_s.length}.max + 2) do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;methods.each do |meth|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.report(meth.to\_s) { n.times do send(meth, \*args, &block) end }  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

# get some data (Ubuntu specific I guess - YMMV)  
array = File.read('/etc/dictionaries-common/words').split(/\n/)

# test w/o dups  
do\_benchmark('no duplicates', 10, [:duplicates\_1, :duplicates\_2,  
:duplicates\_3], array)

# create some duplicates  
array = array[0..999] \* 100  
do\_benchmark('duplicates', 10, [:duplicates\_1, :duplicates\_2,  
:duplicates\_3], array)

\_\_END\_\_  
$ ruby bm-duplicates.rb

> **···**
>
> On 10/28/07, Mohit Sindhwani \<mo\_mail@onghu.com\> wrote:
> 
> > I so have to get the hang of inject, flatten and map.
> > 
> > Cheers,  
> > Mohit.  
> > 10/28/2007 | 9:16 PM.
> 
> ----------------------------------------  
> no duplicates  
> ----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> duplicates\_1 2.200000 0.010000 2.210000 ( 2.215057)  
> duplicates\_2 5.820000 0.000000 5.820000 ( 5.812414)  
> duplicates\_3 6.580000 0.010000 6.590000 ( 6.586708)  
> ----------------------------------------  
> duplicates  
> ----------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> duplicates\_1 1.560000 0.000000 1.560000 ( 1.562587)  
> duplicates\_2 2.660000 0.000000 2.660000 ( 2.665301)  
> duplicates\_3 2.590000 0.000000 2.590000 ( 2.595189)

---

<div class="post-metadata">

**Author:** ![Shuaib\_Zahda](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@Shuaib\_Zahda](https://rubytalk.org/u/Shuaib_Zahda)\
**Post date:** [28 October 2007 13:48 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/8 "2007-10-28T13:48:23Z")

</div>

Thanks a lot guys.  
It works.

I really appreciate your help

Cheers  
Shuaib

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

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [28 October 2007 14:02 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/9 "2007-10-28T14:02:16Z")

</div>

Succint ~and~ efficient! Do you have a mail filter checking for any posts  
containing 'inject'? 🙂

Regards,  
Sean

> **···**
>
> On 10/28/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > irb(main):007:0\> array = %w{apple banana apple orange}  
> > =\> ["apple", "banana", "apple", "orange"]  
> > irb(main):008:0\> array.inject(Hash.new(0)) {|ha,e|  
> > ha[e]+=1;ha}.delete\_if {|k,v| v==1}.keys  
> > =\> ["apple"]
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [28 October 2007 18:15 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/10 "2007-10-28T18:15:20Z")

</div>

Sean O'Halpin wrote:

> **···**
>
> > On 10/28/07, Mohit Sindhwani \<mo\_mail@onghu.com\> wrote:  
> > &nbsp;&nbsp;
> > 
> > > I so have to get the hang of inject, flatten and map.
> > > 
> > > Cheers,  
> > > Mohit.  
> > > 10/28/2007 | 9:16 PM.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;  
> > > Hi,
> > 
> > They are definitely worth looking into - inject in particular is a  
> > powerful tool (Robert Klemme can make it do anything!). However, the  
> > following benchmark shows that a slight modification of your approach  
> > is actually pretty efficient. (The modification is to store the  
> > duplicates in a hash rather than an array so you can return the list  
> > of duplicates using Hash#keys).
> > 
> > Regards,  
> > Sean
> > 
> > # Mohit Sindhwani (with slight adjustment)  
> > def duplicates\_1(array)  
> > &nbsp;&nbsp;seen = { }  
> > &nbsp;&nbsp;duplicates = { }  
> > &nbsp;&nbsp;array.each {|item| seen.key?(item) ? duplicates[item] = true :  
> > seen[item] = true}  
> > &nbsp;&nbsp;duplicates.keys  
> > end
> > 
> > # Robert Klemme  
> > def duplicates\_2(array)  
> > &nbsp;&nbsp;array.inject(Hash.new(0)) {|ha,e| ha[e]+=1;ha}.delete\_if {|k,v| v==1}.keys  
> > end
> > 
> > # from facets  
> > def duplicates\_3(array)  
> > &nbsp;&nbsp;array.inject(Hash.new(0)){|h,v| h[v]+=1; h}.reject{|k,v| v==1}.keys  
> > end
> > 
> > require 'benchmark'
> > 
> > def do\_benchmark(title, n, methods, \*args, &block)  
> > &nbsp;&nbsp;puts '-' \* 40  
> > &nbsp;&nbsp;puts title  
> > &nbsp;&nbsp;puts '-' \* 40  
> > &nbsp;&nbsp;Benchmark.bm(methods.map{ |x| x.to\_s.length}.max + 2) do |x|  
> > &nbsp;&nbsp;&nbsp;&nbsp;methods.each do |meth|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.report(meth.to\_s) { n.times do send(meth, \*args, &block) end }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # get some data (Ubuntu specific I guess - YMMV)  
> > array = File.read('/etc/dictionaries-common/words').split(/\n/)
> > 
> > # test w/o dups  
> > do\_benchmark('no duplicates', 10, [:duplicates\_1, :duplicates\_2,  
> > :duplicates\_3], array)
> > 
> > # create some duplicates  
> > array = array[0..999] \* 100  
> > do\_benchmark('duplicates', 10, [:duplicates\_1, :duplicates\_2,  
> > :duplicates\_3], array)
> > 
> > \_\_END\_\_  
> > $ ruby bm-duplicates.rb  
> > ----------------------------------------  
> > no duplicates  
> > ----------------------------------------  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > duplicates\_1 2.200000 0.010000 2.210000 ( 2.215057)  
> > duplicates\_2 5.820000 0.000000 5.820000 ( 5.812414)  
> > duplicates\_3 6.580000 0.010000 6.590000 ( 6.586708)  
> > ----------------------------------------  
> > duplicates  
> > ----------------------------------------  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > duplicates\_1 1.560000 0.000000 1.560000 ( 1.562587)  
> > duplicates\_2 2.660000 0.000000 2.660000 ( 2.665301)  
> > duplicates\_3 2.590000 0.000000 2.590000 ( 2.595189)
> 
> Thanks Sean! Makes me feel quite nice about it.
> 
> So, hashes are faster than arrays?
> 
> Cheers,  
> Mohit.  
> 10/29/2007 | 2:13 AM.

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 October 2007 04:02 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/11 "2007-10-29T04:02:28Z")

</div>

# $ ruby bm-duplicates.rb  
# ----------------------------------------  
# no duplicates  
# ----------------------------------------  
# user system total real  
# duplicates\_1 2.200000 0.010000 2.210000 ( 2.215057)  
# duplicates\_2 5.820000 0.000000 5.820000 ( 5.812414)  
# duplicates\_3 6.580000 0.010000 6.590000 ( 6.586708)  
# ----------------------------------------  
# duplicates  
# ----------------------------------------  
# user system total real  
# duplicates\_1 1.560000 0.000000 1.560000 ( 1.562587)  
# duplicates\_2 2.660000 0.000000 2.660000 ( 2.665301)  
# duplicates\_3 2.590000 0.000000 2.590000 ( 2.595189)

i just tested this using ruby1.9 on a p4 box running windowsxp. i included ruby's group\_by and got surprising results.

C:\ruby1.9\bin\>diff test-old.rb test.rb  
19a20,24

> #1.9's group\_by  
> def duplicates\_4(array)  
> &nbsp;&nbsp;array.group\_by{|e|e}.select{|\_,k| k.size\>1}.keys  
> end

26c31  
\< Benchmark.bm(methods.map{ |x| x.to\_s.length}.max + 2) do |x|

> **···**
>
> From: Sean O'Halpin [mailto:sean.ohalpin@gmail.com]  
> ---
> 
> > &nbsp;&nbsp;Benchmark.bmbm(methods.map{ |x| x.to\_s.length}.max + 2) do |x|
> 
> 34c39  
> \< array = File.read('/etc/dictionaries-common/words').split(/\n/)  
> ---
> 
> > array = File.read('american-english').split(/\n/)
> 
> 38c43  
> \< :duplicates\_3], array)  
> ---
> 
> > :duplicates\_3,:duplicates\_4], array)
> 
> 43c48  
> \< :duplicates\_3], array)  
> ---
> 
> > :duplicates\_3,:duplicates\_4], array)
> 
> C:\ruby1.9\bin\>
> 
> C:\ruby1.9\bin\>ruby test.rb  
> ----------------------------------------  
> no duplicates  
> ----------------------------------------  
> Rehearsal -------------------------------------------------  
> duplicates\_1 7.609000 0.094000 7.703000 ( 7.984000)  
> duplicates\_2 10.438000 0.109000 10.547000 ( 11.608000)  
> duplicates\_3 14.609000 0.219000 14.828000 ( 14.874000)  
> duplicates\_4 11.422000 0.141000 11.563000 ( 14.201000)  
> --------------------------------------- total: 44.641000sec
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> duplicates\_1 7.219000 0.125000 7.344000 ( 8.109000)  
> duplicates\_2 9.844000 0.078000 9.922000 ( 10.374000)  
> duplicates\_3 14.391000 0.172000 14.563000 ( 18.498000)  
> duplicates\_4 11.172000 0.172000 11.344000 ( 12.998000)  
> ----------------------------------------  
> duplicates  
> ----------------------------------------  
> Rehearsal -------------------------------------------------  
> duplicates\_1 3.375000 0.000000 3.375000 ( 3.765000)  
> duplicates\_2 3.218000 0.000000 3.218000 ( 3.828000)  
> duplicates\_3 3.250000 0.000000 3.250000 ( 3.672000)  
> duplicates\_4 2.032000 0.047000 2.079000 ( 2.077000)  
> --------------------------------------- total: 11.922000sec
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> duplicates\_1 3.375000 0.000000 3.375000 ( 3.437000)  
> duplicates\_2 3.188000 0.000000 3.188000 ( 3.218000)  
> duplicates\_3 3.219000 0.015000 3.234000 ( 3.281000)  
> duplicates\_4 1.844000 0.000000 1.844000 ( 1.859000)
> 
> C:\ruby1.9\bin\>
> 
> kind regards -botp

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [28 October 2007 18:47 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/12 "2007-10-28T18:47:28Z")

</div>

It depends what you're doing with them and how big they are. But in  
this instance, I changed your solution to use a hash because you were  
appending the duplicates to an array which resulted in adding an entry  
to that array every time you detected a duplicate. This didn't show up  
in your example because your data contained at most two instances of  
an item. If you change your example to:

array = ["apple", "banana", "apple", "orange", "fat", "cow", "cow",  
"apple", "apple"]  
h = Hash.new  
duplicates =

array.each {|item|  
&nbsp;&nbsp;&nbsp;if h.has\_key?(item) then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;duplicates \<\< item  
&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[item] = 0 #it doesn't matter what we store  
&nbsp;&nbsp;&nbsp;end  
}

puts duplicates

it outputs

apple  
cow  
apple  
apple

which is probably not what you want.

Regards,  
Sean

> **···**
>
> On 10/28/07, Mohit Sindhwani \<mo\_mail@onghu.com\> wrote:
> 
> > Thanks Sean! Makes me feel quite nice about it.
> > 
> > So, hashes are faster than arrays?
> > 
> > Cheers,  
> > Mohit.  
> > 10/29/2007 | 2:13 AM.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [29 October 2007 10:28 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/13 "2007-10-29T10:28:20Z")

</div>

> \>  
> \> irb(main):007:0\> array = %w{apple banana apple orange}  
> \> =\> ["apple", "banana", "apple", "orange"]  
> \> irb(main):008:0\> array.inject(Hash.new(0)) {|ha,e|  
> \> ha[e]+=1;ha}.delete\_if {|k,v| v==1}.keys  
> \> =\> ["apple"]  
> \>  
> Succint ~and~ efficient!

Thanks!

> Do you have a mail filter checking for any posts  
> containing 'inject'? 🙂

I don't need that since most of them were written by me. 🙂 (slight  
exaggeration)  
\*chuckle\*

Kind regards

robert

> **···**
>
> 2007/10/28, Sean O'Halpin \<sean.ohalpin@gmail.com\>:
> 
> > On 10/28/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Jimmy\_Kofler](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@Jimmy\_Kofler](https://rubytalk.org/u/Jimmy_Kofler)\
**Post date:** [28 October 2007 21:31 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/14 "2007-10-28T21:31:53Z")

</div>

> Duplicate elements in array  
> Posted by Shuaib Zahda (shuaib85) on 28.10.2007 13:47  
> Hello
> 
> I am trying to output the duplicate elements in an array. I looked into  
> the api of ruby I found uniq method which outputs the array with no  
> duplication. What i want is to know which elements is duplicated.

Here's yet another way to do it:  
[http://snippets.dzone.com/posts/show/4148](http://snippets.dzone.com/posts/show/4148)

Cheers,

j.k.

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

---

<div class="post-metadata">

**Author:** ![Mohit\_Sindhwani1](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@Mohit\_Sindhwani1](https://rubytalk.org/u/Mohit_Sindhwani1)\
**Post date:** [29 October 2007 03:44 UTC](https://rubytalk.org/t/duplicate-elements-in-array/41728/15 "2007-10-29T03:44:24Z")

</div>

Sean O'Halpin wrote:

> **···**
>
> > On 10/28/07, Mohit Sindhwani \<mo\_mail@onghu.com\> wrote:  
> > &nbsp;&nbsp;
> > 
> > > Thanks Sean! Makes me feel quite nice about it.
> > > 
> > > So, hashes are faster than arrays?
> > > 
> > > Cheers,  
> > > Mohit.  
> > > 10/29/2007 | 2:13 AM.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;  
> > > It depends what you're doing with them and how big they are. But in  
> > > this instance, I changed your solution to use a hash because you were  
> > > appending the duplicates to an array which resulted in adding an entry  
> > > to that array every time you detected a duplicate. This didn't show up  
> > > in your example because your data contained at most two instances of  
> > > an item. If you change your example to:
> > 
> > array = ["apple", "banana", "apple", "orange", "fat", "cow", "cow",  
> > "apple", "apple"]  
> > h = Hash.new  
> > duplicates =
> > 
> > array.each {|item|  
> > &nbsp;&nbsp;&nbsp;if h.has\_key?(item) then  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;duplicates \<\< item  
> > &nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[item] = 0 #it doesn't matter what we store  
> > &nbsp;&nbsp;&nbsp;end  
> > }
> > 
> > puts duplicates
> > 
> > it outputs
> > 
> > apple  
> > cow  
> > apple
> > 
> > which is probably not what you want.
> > 
> > Regards,  
> > Sean
> 
> Thanks for the explanation, Sean. Actually, I guess it's not clear if the OP wants to know each occurrence of the duplicates or just the list of duplicates. But, there are now solutions for both cases!
> 
> Cheers,  
> Mohit.  
> 10/29/2007 | 11:44 AM.
