Skip to content

Instantly share code, notes, and snippets.

@axelarge
Created June 5, 2013 05:19
Show Gist options
  • Save axelarge/5711773 to your computer and use it in GitHub Desktop.
Save axelarge/5711773 to your computer and use it in GitHub Desktop.
class Array
def pairs_for(k)
seen = Hash.new(0)
each_with_object([]) do |i, result|
complement = k - i
seen_count = seen[complement]
if seen_count > 0
result << [complement, i]
seen[complement] -= 1
else
seen[i] += 1
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment