Q:

Write a Scala program to find the second largest element from a given array of integers

0

Write a Scala program to find the second largest element from a given array of integers.

All Answers

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

object Scala_Array {   
   def main(args: Array[String]): Unit = {     
       var my_array = Array(10789, 2035, 1899, 1456, 2013,1458, 2458,
                            1254, 1472, 2365,1456, 2165, 1457, 2456);
       println("Orginal array:")
       for ( x <- my_array) {
         print(s"${x}, ")        
        }      
     scala.util.Sorting.quickSort(my_array)
     var index = my_array.length-1;
     while(my_array(index)==my_array(my_array.length-1)){
       index=index-1
       }     
     println("\nSecond largest value of the said array: " + my_array(index));      
   }
 }

Sample Output:

Orginal array:
10789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165, 1457, 2456, 
Second largest value of the said array: 2458

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