find Kth largest element in array js tc code example
Example 1: get largest number in array javascript
const array1 = [1, 3, 2];
Math.max(...array1);
// expected output: 3
Example 2: find second largest number in array javascript
var secondMax = function (arr){
var max = Math.max.apply(null, arr), // get the max of the array
maxi = arr.indexOf(max);
arr[maxi] = -Infinity; // replace max in the array with -infinity
var secondMax = Math.max.apply(null, arr); // get the new max
arr[maxi] = max;
return secondMax;
};