# Re: Isolating non-unique items in an array

**URL:** <https://www.ruby-forum.com/t/re-isolating-non-unique-items-in-an-array/72617>\
**Category:** Ruby\
**Created:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/re-isolating-non-unique-items-in-an-array/72617 "2006-10-18T10:39:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/re-isolating-non-unique-items-in-an-array/72617/1 "2006-10-18T10:39:19Z")

</div>

> a = [“a”, “a”, “a”, “b”, “c”, “d”, “d”]  
> p a.duplicates\_count  
> #=\> [{“a”=\>3}, {“d”=\>2}]

Don’t listen to that Phrogz character, he’s insane!

More like:  
class Array  
def duplicates\_count  
Hash[ \*uniq.map{ |e|  
if ( count = grep(e).size ) \> 1  
[e, count]  
end  
}.compact.flatten ]  
end  
end

a = [“a”, “a”, “a”, “b”, “c”, “d”, “d”]  
p a.duplicates\_count  
#=\> {“a”=\>3, “d”=\>2}

But of course, other posts in this thread are likely faster.
