Skip to content

Instantly share code, notes, and snippets.

@kisai
Created August 20, 2014 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kisai/5a217c95cf2daf261bef to your computer and use it in GitHub Desktop.
Save kisai/5a217c95cf2daf261bef to your computer and use it in GitHub Desktop.
Find duplicates in a array
ary = ["A", "B", "C", "B", "A"]
ary.group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
ary.sort.chunk { |e| e }.select { |e, count| count.size > 1 }.map(&:first)
ary.select { |e| ary.count(e) > 1 }.uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment