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.

Example 1:
Input: nums = [1,3,4,2,2]
Output: 2

Example 2:
Input: nums = [3,1,3,4,2]
Output: 3

Example 3:
Input: nums = [1,1]
Output: 1

Example 4:
Input: nums = [1,1,2]
Output: 1

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