find second largest number in array javascript
var secondMax = function (){
var arr = [20, 120, 111, 215, 54, 78]; // use int arrays
var max = Math.max.apply(null, arr); // get the max of the array
arr.splice(arr.indexOf(max), 1); // remove max from the array
return Math.max.apply(null, arr); // get the 2nd max
};
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;
};
Also in JavaScript:
- jquery get element innertext
- insert item into array specific index javascript
- hide url in discord.js
- javascript getdate
- how to get a toggle button to do different js functions
- initialize express app
- empty input of clone jquery
- string.find javascript
- javascript hide div
- random image and link js
- remove object from array by name javascript
- how to create a server in node js
- jquery get textarea value
- how to create variables using javascript
- what is javascript
- vuejs components props pass array
- axios get array of urls
- facebook pixel
- discord.js download
- let fruta angular
- es6 compare two arrays
- bookshelf insert multiple rows
- split a message
- fetch composition API in Vue3