From ba03727fb6cef1ab4a06f994ffe549030f53f163 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 15 Mar 2021 09:52:22 -0700 Subject: [PATCH] Add solution using delete_if Ruby method --- data_structures/arrays/remove_elements.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/remove_elements.rb b/data_structures/arrays/remove_elements.rb index e407c7b..84abca7 100644 --- a/data_structures/arrays/remove_elements.rb +++ b/data_structures/arrays/remove_elements.rb @@ -14,5 +14,16 @@ # Output: 5, nums = [0,1,4,0,3] # -# Approach 1: Brute Force -# \ No newline at end of file +# 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 \ No newline at end of file