Created
March 14, 2018 15:18
-
-
Save AnielaMW/b867bef1fbabac659a87fb7b0b5f59e2 to your computer and use it in GitHub Desktop.
First Duplicate Challenge
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
require 'pry' | |
def first_duplicate1(a) | |
counts = [] | |
a.each_with_index do |value, i| | |
return value if counts[value] | |
counts[value] = true | |
binding.pry | |
end | |
-1 | |
end | |
def first_duplicate2(a) | |
a.each do |value| | |
return value.abs if a[value.abs - 1] < 0 | |
a[value.abs - 1] = -a[value.abs - 1] | |
end | |
-1 | |
end | |
array1 = [2, 3, 3, 1, 5, 2] | |
array2 = [2, 4, 3, 5, 1] | |
array3 = [1] | |
array4 = [8, 4, 7, 9, 5, 6, 2, 6, 4, 8] | |
# puts first_duplicate1(array1) == 3; | |
# puts first_duplicate1(array2) == -1; | |
# puts first_duplicate1(array3) == -1; | |
puts first_duplicate1(array4) == 6; | |
# | |
# puts first_duplicate2(array1) == 3; | |
# puts first_duplicate2(array2) == -1; | |
# puts first_duplicate2(array3) == -1; | |
# puts first_duplicate2(array4) == 6; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment