Skip to content

Instantly share code, notes, and snippets.

@stevenbarragan
Last active January 3, 2016 12:19
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 stevenbarragan/8461802 to your computer and use it in GitHub Desktop.
Save stevenbarragan/8461802 to your computer and use it in GitHub Desktop.
Emulate array insertion but allowing repeated numbers
def intersection(a, b)
hash = {}
c = []
a.each do |x|
hash[x] ? hash[x] += 1 : hash[x] = 1
end
b.each do |x|
c << x && hash[x] -= 1 if hash[x] && hash[x] > 0
end
c
end
describe '#insertion' do
context 'positive numbers' do
let(:a){ [3,6,5,12,9,4,6] }
let(:b){ [6,8,6,3,1,1] }
it 'gets the insertion' do
expect(insertion(a,b)).to eq [6,6,3]
end
end
context 'negative numbers' do
let(:a){ [3,-6,-5,12,9,4,6,-5] }
let(:b){ [-6,-5,6,3,1,1,-5] }
it 'gets the insertion' do
expect(insertion(a,b)).to eq [-6, -5, 6, 3, -5]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment