We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def end_arr_add(arr, element)
# Add element to the end of the Array variable arr and return arr
return 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)
return arr
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)
return arr
end
def index_arr_multiple_add(arr, index)
# add any two elements to the arr at the index
arr.insert(index,20,30)
return arr
end
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ruby Array - Addition
You are viewing a single comment's thread. Return to all comments →
def end_arr_add(arr, element) # Add
element
to the end of the Array variablearr
and returnarr
return arr.push(element) enddef begin_arr_add(arr, element) # Add
element
to the beginning of the Array variablearr
and returnarr
arr.unshift(element) return arr enddef index_arr_add(arr, index, element) # Add
element
at positionindex
to the Array variablearr
and returnarr
arr.insert(index,element) return arr enddef index_arr_multiple_add(arr, index) # add any two elements to the arr at the index arr.insert(index,20,30) return arr end