Posted in

1. The second largest number in Array

This is one of the mostly asked interview problem. Solution given in kotlin,

fun main() {
  val nums = arrayOf(6){1,2,3,4,5,6}
  println(output(nums))
}

fun output(arr : Array<Int>) { 
   var max = INT.MIN_VALUE
   var secondMax = INT.MIN_VALUE
   for( x in arr.indices) {
      if(x > max) {
         secondMax = max
         max = x
      }
      else if(x > secondMax) {
         secondMax = x
      }
   }
}

/** output:
* 2
**/