Hackerrank Ruby Array - Addition Solution
2 min read

Hackerrank Ruby Array - Addition Solution

Hackerrank Ruby Array - Addition Solution

.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}

Arrays provide a variety of methods that allow to add elements to them.

  • push allows one to add an element to the end of the list. >x = [1,2] >x.push(3) => [1,2,3]
  • insert allows one to add one or more elements starting from a given index (shifting elements after the given index in the process). >x = [1,2] >x.insert(1, 5, 6, 7) => [1, 5, 6, 7, 2]
  • unshift allows one or more elements to be added at the beginning of the list. >x = [1,2,3] >x.unshift(10, 20, 30) => [10, 20, 30, 1, 2, 3]

In this challenge, your task is to complete three functions that take in the array arr and

  1. Add an element to the end of the list
  2. Add an element to the beginning of the list
  3. Add an element after a given index (position)
  4. Add more than one element after a given index (position)

Solution in ruby

Approach 1.

def end_arr_add(arr, element)
    arr.push(element)
    arr
end

def begin_arr_add(arr, element)
    arr.unshift(element)
    arr
end

def index_arr_add(arr, index, element)
    arr.insert(index, element)
    arr
end

def index_arr_multiple_add(arr, index)
    arr.insert(index, 6, 9)
    arr
end

Approach 2.

def end_arr_add(arr, element)
  arr << element  # Add `element` to the end of the Array variable `arr` and return `arr`
end

def begin_arr_add(arr, element)
  arr.unshift(element)  # Add `element` to the beginning of the Array variable `arr` and return `arr`
end

def index_arr_add(arr, index, element)
  arr.insert(index, element)  # Add `element` at position `index` to the Array variable `arr` and return `arr`
end

def index_arr_multiple_add(arr, index)
  arr.insert(index, 1, 2)
end

Approach 3.

def end_arr_add(arr, element)
    # Add `element` to the end of the Array variable `arr` and return `arr`
    arr.push(element)
end

def begin_arr_add(arr, element)
    # Add `element` to the beginning of the Array variable `arr` and return `arr`
    arr.unshift(element)
end

def index_arr_add(arr, index, element)
    # Add `element` at position `index` to the Array variable `arr` and return `arr`
    arr.insert(index,element)
end

def index_arr_multiple_add(arr, index)
    # add any two elements to the arr at the index
    arr.insert(index,1,2)
end

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×