Add solution using Ruby .count()

This commit is contained in:
Jessica Kwok 2021-03-22 08:53:26 -07:00
parent e70ba583a2
commit e8ded18897

View file

@ -38,3 +38,24 @@ puts(single_number(nums))
nums = [1]
puts(single_number(nums))
# Output: 1
#
# Approach 2: Use Ruby .count()
#
# Time Complexity: O(1)
#
def single_number(nums)
nums.find do |num|
nums.count(num) == 1
end
end
nums = [2, 2, 1]
puts(single_number(nums))
# Output: 1
nums = [4, 1, 2, 1, 2]
puts(single_number(nums))
# Output: 4
nums = [1]
puts(single_number(nums))
# Output: 1