Created
November 16, 2011 20:12
-
-
Save ryanlecompte/1371212 to your computer and use it in GitHub Desktop.
Ways to find duplicates in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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
Another couple of solutions:
I think which one is fastest depend on the array length.