HackerRank Ruby Array Deletion Solution

Hello coders, In this post, you will learn how to solve HackerRank Ruby Array Deletion Solution. This problem is a part of the Ruby Tutorial series. One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

HackerRank Ruby Array Deletion Solution
HackerRank Ruby Array Deletion Solution

HackerRank Ruby Array Deletion

Let’s get started with Ruby Array Deletion Solution

Problem Statement

The array class has various methods of removing elements from the array.

Lets look at the array

 arr = [5, 6, 5, 4, 3, 1, 2, 5, 4, 3, 3, 3] 
  • Delete an element from the end of the array
 > arr.pop
 => 3
  • Delete an element from the beginning of the array
 > arr.shift
 => 5
  • Delete an element at a given position
 > arr.delete_at(2)
 => 4
  • Delete all occurrences of a given element
 > arr.delete(5)
 => 5
 > arr
 => [6, 3, 1, 2, 4, 3, 3]

Your task is to complete the functions below using syntax as explained above.

HackerRank Ruby Array Deletion Solution

def end_arr_delete(arr)
    # delete the element from the end of the array and return the deleted element
    arr.pop
end
def start_arr_delete(arr)
    # delete the element at the beginning of the array and return the deleted element
    arr.shift
end
def delete_at_arr(arr, index)
    # delete the element at the position #index
  arr.delete_at(index)
end
def delete_all(arr, val)
    # delete all the elements of the array where element = val
  arr.delete(val)
end

Note: This problem (Ruby Array – Deletion) is generated by HackerRank but the solution is provided by Chase2Learn. This tutorial is only for Educational and Learning purpose.

Sharing Is Caring