
Find the Second Largest Number in an Array by JavaScript.
using splice()and indexOf() methods
Hello everyone!!,This is my first post on medium feeling good! lets get into topic, find the second largest number in a array, let’s write a function to find “Second largest number” from a array
In the main function we will use Math.max() and we will also use Array.splice() and Array.indexof() methods
Math.max() is used to find the largest number from an array
var arr = [1, 2, 3];
var max = Math.max(...arr);
Array.splice() is used to add or remove specific elements from array for more examples on Array.splice() check the link below
Array=[1,2,3,4,5]
Array.splice(1,1)//output
Array=[1,3,4,5]
Array.indexof() is used to find the index of a specific element in an array
Array=[1,2,3,4,5]
Array.indexOf(5)//ouput//
4
down is the code of main function processData(),go through the comments in the code for detailed understanding
The below source code includes complete code ,which includes function math.max() ,and we called that function in our main function processData
If you like what I’m doing here, please give this article a clap 👏 !