Q:

Write a Scala program to check if the value of the fast or last element of a given array ( length 1 or more) are same or not

0

Write a Scala program to check if the value of the fast or last element of a given array ( length 1 or more) are same or not.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object  Scala_Array {
  
   def test(nums: Array[Int]) : Boolean = {
    if (nums.length < 1) false 
     else nums.head == nums.last
    }        
  def main(args: Array[String]): Unit = {     
    println("Check fast or last element of a given array are same or not!") 
    var nums1 = Array(1,2,3,4,5,6)
     println("Original Array elements:")
     // Print all the array elements
      for ( x <- nums1 ) {
         print(s"${x}, ")        
       }
      println("\nResult: "+test(nums1))
     var nums2 = Array(1,2,3,4,5,1)
     var n2= 1;
     println("Original Array elements:")
     // Print all the array elements
      for ( x <- nums2 ) {
         print(s"${x}, ")        
       }
      println("\nResult: "+test(nums2))
       }
}

Sample Output:

Check fast or last element of a given array are same or not!
Original Array elements:
1, 2, 3, 4, 5, 6, 
Result: false
Original Array elements:
1, 2, 3, 4, 5, 1, 
Result: true

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now