Ruby Array Exercises: Check whether all items are identical in a given array
Ruby Array: Exercise-44 with Solution
Write a Ruby program to check whether all items are identical in a given array.
Ruby Code:
num1 = [10, 20, 30, 40, 10, 10, 20]
print "Original array:\n"
print num1
print "\nIs all items are identical?\n"
print num1.all? {|x| x == num1[0]}
num2 = [10, 10, 10]
print "\nOriginal array:\n"
print num2
print "\nIs all items are identical?\n"
print num2.all? {|x| x == num2[0]}
Output:
Original array: [10, 20, 30, 40, 10, 10, 20] Is all items are identical? false Original array: [10, 10, 10] Is all items are identical? true
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous:Write a Ruby program to find most occurred item in a given array.
Next: Write a Ruby program to search items start with specified string of a given array.
What is the difficulty level of this exercise?
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-44.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics