mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
Add solution using delete_if Ruby method
This commit is contained in:
parent
9429ae92c1
commit
ba03727fb6
1 changed files with 13 additions and 2 deletions
|
@ -14,5 +14,16 @@
|
|||
# Output: 5, nums = [0,1,4,0,3]
|
||||
|
||||
#
|
||||
# Approach 1: Brute Force
|
||||
#
|
||||
# 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
|
Loading…
Reference in a new issue