Returns true if the input array has any duplicates, false otherwise.
[1,2,3,1] → true
[1,2,3,4] → false
# 🔁 2. Contains Duplicate
# Returns true if the input array has any duplicates, false otherwise.
# Uses a Set to keep track of seen elements.
# Time Complexity: O(n)
# Why: Single pass through the array while checking and adding to a Set.
def contains_duplicate(nums)
seen = Set.new
nums.each do |num|
return true if seen.include?(num)
seen.add(num)
end
false
O(n)
O(n)