Created
November 26, 2013 01:09
-
-
Save jgaskins/7651822 to your computer and use it in GitHub Desktop.
Remove one and only one occurrence of each specified value from an array
This file contains hidden or 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
class Array | |
def remove_one values | |
values = values.dup | |
new_array = [] | |
each do |value| | |
if values.include? value | |
values.delete value | |
else | |
new_array << value | |
end | |
end | |
new_array | |
end | |
end |
This file contains hidden or 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
describe 'Array#remove_one' do | |
it 'removes one of each value from the array' do | |
[1, 1, 2, 3].remove_one([1, 2]).should == [1, 3] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment