LeetCode: Find the Duplicate Number
Question
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
Solution#1
def find_duplicate(nums)
nums.each do |num|
return num if nums.count(num) >= 2
end
end
Solution#2
def find_duplicate(nums)
dict = {}
nums.each do |num|
if dict.has_key?(num)
return num
else
dict[num] = num
end
end
end
Comments