Skip to content

Instantly share code, notes, and snippets.

@thinca
Last active August 29, 2015 14:08
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 thinca/84a998fd62cafe0df739 to your computer and use it in GitHub Desktop.
Save thinca/84a998fd62cafe0df739 to your computer and use it in GitHub Desktop.
[[1,2],[2,3],[6,7,8],[7,9],[100,1000]].each_with_object([{}, []]) {|a, (o, r)|
e = a.find {|i| o.include?(i) }
if e
o[e].concat(a)
else
r.push(a)
end
x = o[e] || a
a.each do |i|
o[i] = x
end
}[1].map(&:uniq).display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment