- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the second largest element in a user-input JavaScript array?
Let’s say following is our array −
var numbers=[10,50,80,60,89];
To find the second largest element, the code is as follows −
Example
var numbers=[10,50,80,60,89]; var firstLargerNumber = Number.MIN_SAFE_INTEGER; var secondlargerNumber = firstLargerNumber; for(var tempNumber of numbers){ if(tempNumber > firstLargerNumber){ secondlargerNumber = firstLargerNumber; firstLargerNumber = tempNumber; } else if(tempNumber > secondlargerNumber){ secondlargerNumber = tempNumber; } } console.log("The second largest number="+secondlargerNumber);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo138.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo138.js The second largest number=80
- Related Articles
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Find the second most frequent element in array JavaScript
- Find the Second Largest Element in a Linked List in C++
- How to find the largest number contained in a JavaScript array?
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Swift Program to Find Largest Array Element
- Python Program to find the largest element in an array
- C program to find the second largest and smallest numbers in an array
- MongoDB Aggregate Second Element from Input Element?
- Finding the second largest element in BST using C++
- Python Program to find largest element in an array
- How Do I Find the Largest Number in a 3D JavaScript Array?
- C# Program to find the largest element from an array
- Program to find largest element in an array in C++

Advertisements