Write a Scala program to find the second smallest element from a given array of integers.
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}, ") } var min = Int.MaxValue var second_min = Int.MaxValue for (i <- 0 to my_array.length-1) { if(my_array(i)==min){ second_min=min; } else if (my_array(i) < min) { second_min = min; min = my_array(i); } else if (my_array(i) < second_min) { second_min = my_array(i); } } println("\nSecond lowest value of the said array: " + second_min); } }
Sample Output:
Orginal array: 10789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165, 1457, 2456, Second lowest value of the said array: 1456
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer