Saturday, 22 February 2020

Second Max Number

Second Max Number


Objective

Given an array, we have to find the second max element of an array

Sample Input

[32,5,26,78,33,1]

Sample Output

33

Code

Here's my solution with the embedded comments.



arr=[32,5,26,78,33,1]

arr.sort(function(a,b){
  return a-b
})

len=arr.length

console.log(arr[len-2]) 


I hope you found this helpful.
Cheers :)

No comments:

Post a Comment

Diagonal Difference

Diagonal Difference Objective Given a square matrix, calculate the absolute difference between the sums of its diagonals. Descripti...