Created
May 23, 2015 17:31
-
-
Save kareemgrant/4f708c3cffffbccb76f6 to your computer and use it in GitHub Desktop.
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
# Given an array of n numbers with exactly one duplicate, write a program that finds the duplicate number in an efficient manner | |
require 'minitest/autorun' | |
def find_duplicate(numbers_array) | |
# using an array as a way to keep track of evaluted numbers | |
checked_numbers = [] | |
numbers_array.each do |number| | |
return number if checked_numbers.include?(number) | |
checked_numbers << number | |
end | |
'no duplicate found' | |
end | |
class FindDuplicatesTest < MiniTest::Unit::TestCase | |
def test_returns_message_with_no_duplicates | |
assert_equal 'no duplicate found', find_duplicate([1, 2, 3].shuffle) | |
end | |
def test_returns_duplicate_with_small_array | |
assert_equal 3, find_duplicate([1, 2, 3, 3].shuffle) | |
end | |
def test_returns_duplicate_with_larger_array | |
assert_equal 9999, find_duplicate((1..9999).to_a.shuffle << 9999) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment