Ruby Array Exercises: Check whether first and the last element are the same of a given array of integers
Ruby Array: Exercise-4 with Solution
Write a Ruby program to check whether first and the last element are the same of a given array of integers. The array length must be 1 or more.
Ruby Code:
def check_array(nums)
return (nums.length >= 1 && nums[0] == nums[nums.length-1])
end
print check_array([1, 2, 7]),"\n"
print check_array([3, 1, 2, 3]),"\n"
print check_array([14, 7, 1, 2, 3])
Output:
false true false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to pick number of random elements from an given array.
Next: Write a Ruby program to compute the sum of elements in a given array.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework