w3resource

Ruby Array Exercises: Check two given arrays of integers and test whether they have the same first element or they have the same last element

Ruby Array: Exercise-7 with Solution

Write a Ruby program to check two given arrays of integers and test whether they have the same first element or they have the same last element. Both arrays length must be 1 or more.

Ruby Array Exercises: Check two given arrays of integers and test whether they have the same first element or they have the same last element

Ruby Code:

def check_array(a, b)
   return (a[0] == b[0] || a[a.length-1] == b[b.length-1])
end
print check_array([1, 2, 5], [7, 5]),"\n" 
print check_array([1, 2, 3], [7, 3, 2]),"\n" 
print check_array([1, 2, 4], [1, 4]) 

Output:

true
false
true

Flowchart:

Flowchart: Check two given arrays of integers and test whether they have the same first element or they have the same last element

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to remove duplicate elements from a given array.
Next: Write a Ruby program to remove blank elements from a given array.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/array/ruby-array-exercise-7.php