Scala Programming: Check the value of the fast or last element of two given array of integers are same or not
Scala Programming Array Exercise-6 with Solution
Write a Scala program to check whether the value of the fast or last element of two given array ( length 1 or more) of integers are same or not.
Sample Solution:
Scala Code:
object Scala_Array
{
def test(x: Array[Int], y: Array[Int]): Boolean = {
if (x.length < 1 || y.length < 1) false
else (x.head == y.head) || (x.last == y.last)
}
def main(args: Array[String]): Unit =
{
println(test(Array(1,2,3,4), Array(2,3,4)))
println(test(Array(1,2,3,4), Array(1,3,4)))
println(test(Array(1,2,3,4), Array(1,3,4,5)))
println(test(Array(2,2,3,4), Array(1,3,4,5)))
}
}
Sample Output:
true true true false
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to find the index of an element in a given Array.
Next: Write a Scala program to remove a specific element from an 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://w3resource.com/scala-exercises/array/scala-array-exercise-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics