mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Add solution using delete_at Ruby method
This commit is contained in:
parent
ba03727fb6
commit
1f95ad15df
1 changed files with 26 additions and 1 deletions
|
@ -26,4 +26,29 @@ end
|
|||
puts remove_elements([3,2,2,3], 3)
|
||||
# => 2
|
||||
puts remove_elements([0,1,2,2,3,0,4,2], 2)
|
||||
# => 5
|
||||
# => 5
|
||||
|
||||
#
|
||||
# Approach 2: Use `delete_at`, `unshift`, and `shift` Ruby method
|
||||
#
|
||||
# Time complexity: O(n)
|
||||
#
|
||||
def remove_elements(nums, val)
|
||||
result_length = nums.length
|
||||
shift_length = 0
|
||||
nums.each_with_index do |num, i|
|
||||
if num == val
|
||||
nums.delete_at(i)
|
||||
nums.unshift('removed')
|
||||
result_length -=1
|
||||
shift_length += 1
|
||||
end
|
||||
end
|
||||
nums.shift(shift_length)
|
||||
result_length
|
||||
end
|
||||
|
||||
puts remove_elements([3,2,2,3], 3)
|
||||
# => 2
|
||||
puts remove_elements([0,1,2,2,3,0,4,2], 2)
|
||||
# => 5
|
||||
|
|
Loading…
Reference in a new issue