You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank Ruby Array - Deletion Solution

Hackerrank Ruby Array - Deletion Solution

Beeze Aal
Beeze Aal

.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue}

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

Let's 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.

Solution in ruby

Approach 1.

def end_arr_delete(arr)
    arr.pop
    
end

def start_arr_delete(arr)
    arr.shift
    
end

def delete_at_arr(arr, index)
    arr.delete_at(index)
    
end

def delete_all(arr, val)
    arr.delete(val)
    
end

Approach 2.

def end_arr_delete(arr)
  arr.pop
  # delete the element from the end of the array and return the deleted element
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)
  arr.delete(val)
    # delete all the elements of the array where element = val
end

Approach 3.

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