Re: Isolating non-unique items in an array

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs