In this post, we will solve Ruby Array – Initialization HackerRank Solution. This problem (Ruby Array – Initialization) is a part of HackerRank Ruby series.
One of the most commonly used data structures in Ruby is a Ruby Array, and below we see various methods of initializing a ruby array.
Your task is to initialize three different variables as explained below.
- Initialize an empty array with the variable name
array = Array.new
array = 
- nitialize an array with exactly one
nilelement in it with the variable name
array_1 = Array.new(1)
array_1 = [nil]
- Initialize an array with exactly two elements with value
10in it using the variable name
array_2 = Array.new(2, 10)
array_2 = [10, 10]
Solution – Ruby Array – Initialization Solution
# Initialize 3 variables here as explained in the problem statement array = Array.new(0) array_1 = Array.new(1) array_2 = Array.new(2,10)