mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-30 20:34:53 +01:00
Add single number challenge
This commit is contained in:
parent
fdfb4fff55
commit
935faca63a
1 changed files with 22 additions and 0 deletions
22
data_structures/arrays/single_number.rb
Normal file
22
data_structures/arrays/single_number.rb
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
# Challenge name: Single Number
|
||||||
|
#
|
||||||
|
# Given a non-empty array of integers nums, every element appears twice
|
||||||
|
# except for one. Find that single one.
|
||||||
|
#
|
||||||
|
# Follow up: Could you implement a solution with a linear runtime
|
||||||
|
# complexity and without using extra memory?
|
||||||
|
#
|
||||||
|
# @param {Integer[]} nums
|
||||||
|
# @return {Integer}
|
||||||
|
|
||||||
|
def single_number(nums)
|
||||||
|
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
|
Loading…
Add table
Reference in a new issue