Last active
January 3, 2016 12:19
-
-
Save stevenbarragan/8461802 to your computer and use it in GitHub Desktop.
Emulate array insertion but allowing repeated numbers
This file contains 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
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