Skip to content

Instantly share code, notes, and snippets.

@troyleach
Last active August 29, 2015 14:22
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 troyleach/820e5f536431ecffb20e to your computer and use it in GitHub Desktop.
Save troyleach/820e5f536431ecffb20e to your computer and use it in GitHub Desktop.
Return the INDEX of the first duplicate number in an array of number
# Hello Marc, so I just couldn't let our challenge go. after I left I kept thinking
# about the solution that I provided and the fact that it wouldn't work. So tonight
# I decided to code it and write some test for it and I wanted to share it with you.
# If I copied the array and then deleted the elements the program would always return 0
# because that is where the duplicate number would live at the time it got compaired.
# I would love some feedback and would also love to see how you might solve this.
def find_duplicate_retrun_index(arr)
arrCopy = arr
arr.each_with_index do |value1, index1|
arrCopy.each_with_index do |value2, index2|
if index1 == index2 || value1 != value2
arrCopy.delete(0)
else
return index1
end
end
end
end
## RSpec file finddup_spec.rb
require_relative 'finddup'
describe 'find_duplicate_retrun_index' do
let(:numbers) { [1, 2, 3, 4, 5, 5, 6, 7] }
let(:numbers1) { [1, 2, 3, 4, 5, 8, 6, 10, 10] }
let(:numbers2) { [7, 1, 2, 3, 4, 5, 8, 6, 7] }
it "method is defined" do
expect(defined? find_duplicate_retrun_index).to eq 'method'
end
it "method takes a single argument" do
expect(method(:find_duplicate_retrun_index).arity).to eq 1
end
it 'Returns the index of the first duplicate number' do
expect(find_duplicate_retrun_index(numbers)).to eq 4
end
it 'returns the index of the first duplicate number' do
expect(find_duplicate_retrun_index(numbers1)).to eq 7
end
it 'returns the index of the first duplicate number' do
expect(find_duplicate_retrun_index(numbers2)).to eq 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment