Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Created November 16, 2011 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlecompte/1371212 to your computer and use it in GitHub Desktop.
Save ryanlecompte/1371212 to your computer and use it in GitHub Desktop.
Ways to find duplicates in an array
ary = [1,1,1,1,2,3,4,5,5,5,5,5,5,10,10,100]
# approach 1
ary.enum_for(:sort).with_object([]) { |(a,b), result| result << a if a == b; a <=> b}.uniq
# approach 2
ary.select { |e| ary.count(e) > 1 }.uniq
# approach 3 (from @apeiros)
ary.group_by {|e| e}.select { |k, v| v.size > 1 }.keys
# approach 4
ary.sort.chunk { |i| i}.select { |i, chunk| chunk.size > 1 }.map(&:first)
@afcapel
Copy link

afcapel commented Nov 16, 2011

Another couple of solutions:

ary.uniq.select { |i| ary.delete_at(ary.index(i)) && ary.delete_at(ary.index(i) || ary.length) }

ary.uniq.delete_if { |i| ary.one? { |elm| elm == i } }

I think which one is fastest depend on the array length.

@semmons99
Copy link

 a.partition{|n| a.select{|x| x == n}.length > 1}.first.uniq

I did some benchmarking and found the fastest approach to be #3, nearly twice as fast as the first two solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment