
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Return Top two elements from array JavaScript
We have an array of numbers in JavaScript that contains numbers in an unsorted order. Our job is to write a function that takes in this array of numbers and returns an array of two elements, the top two elements of the array (greatest two elements of the array).
We have to do this in one pass i.e., we need to execute this method in linear time like by using only one for loop or if we use ES6 function, we have to make sure to use only one and once and avoid nesting of methods which increases time complexity.
So, now let's write the code using the Array.prototype.reduce() method −
Example
const arr = [23, 65, 67, 23, 2, 6, 87, 23, 45, 65, 3, 234, 3]; const topTwo = arr => { if(arr.length < 2){ return false; }; return arr.reduce((acc, val) => { if(val > acc[0]){ let t = acc[0]; acc[0] = val; acc[1] = t; }else if(val > acc[1]){ acc[1] = val; }; return acc; }, [-Infinity, -Infinity]); }; console.log(topTwo(arr));
Output
The output in the console will be −
[ 234, 87 ]
- Related Articles
- Return the sum of two consecutive elements from the original array in JavaScript
- Remove elements from array using JavaScript filter - JavaScript
- Top n max value from array of object JavaScript
- Removing redundant elements from array altogether - JavaScript
- How to swap two array elements in JavaScript?
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Counting below / par elements from an array - JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Return the first duplicate number from an array in JavaScript
- Add two consecutive elements from the original array and display the result in a new array with JavaScript
- JavaScript - How to pick random elements from an array?
- Picking the largest elements from multidimensional array in JavaScript
- Return the bases when first array elements are raised to powers from second array in Python
- How to filter an array from all elements of another array – JavaScript?
- Remove elements from array in JavaScript using includes() and splice()?

Advertisements