mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-28 22:24:14 +01:00
Merge pull request #99 from jsca-kwok/jk-remove-element
Remove a given value from an array
This commit is contained in:
commit
d0edcd506f
2 changed files with 56 additions and 1 deletions
|
@ -9,7 +9,8 @@
|
|||
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
|
||||
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
|
||||
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb)
|
||||
* [Two Sum Ii](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb)
|
||||
* [Two Sum II](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb)
|
||||
* [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb)
|
||||
* Binary Trees
|
||||
* [Inorder Traversal](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/inorder_traversal.rb)
|
||||
* [Invert](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/binary_trees/invert.rb)
|
||||
|
|
54
data_structures/arrays/remove_elements.rb
Normal file
54
data_structures/arrays/remove_elements.rb
Normal file
|
@ -0,0 +1,54 @@
|
|||
# Arrays - Remove Elements
|
||||
#
|
||||
# Given an array nums and a value val, remove all instances of that value in-place and return the new length.
|
||||
# Do not allocate extra space for another array,
|
||||
# you must do this by modifying the input array in-place with O(1) extra memory.
|
||||
# The order of elements can be changed. It doesn't matter what you leave beyond the new length.
|
||||
#
|
||||
# Example
|
||||
#
|
||||
# Input: nums = [3,2,2,3], val = 3
|
||||
# Output: 2, nums = [2,2]
|
||||
#
|
||||
# Input: nums = [0,1,2,2,3,0,4,2], val = 2
|
||||
# Output: 5, nums = [0,1,4,0,3]
|
||||
|
||||
#
|
||||
# Approach 1: Use `delete_if` Ruby method
|
||||
#
|
||||
# Time complexity: O(n)
|
||||
#
|
||||
def remove_elements(nums, val)
|
||||
nums.delete_if{ |num| num == val }
|
||||
nums.length
|
||||
end
|
||||
|
||||
puts remove_elements([3,2,2,3], 3)
|
||||
# => 2
|
||||
puts remove_elements([0,1,2,2,3,0,4,2], 2)
|
||||
# => 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