
- 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 indexes of greatest values in an array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The array may contain more than one greatest element (i.e., repeating greatest element).
We are required to write a JavaScript function that takes in one such array and returns all the indices of the greatest element.
Example
The code for this will be −
const arr = [10, 5, 4, 10, 5, 10, 6]; const findGreatestIndices = arr => { const val = Math.max(...arr); const greatest = arr.reduce((indexes, element, index) => { if(element === val){ return indexes.concat([index]); } else { return indexes; }; }, []); return greatest; } console.log(findGreatestIndices(arr));
Output
And the output in the console will be −
[ 0, 3, 5 ]
- Related Articles
- Find indexes of multiple minimum value in an array in JavaScript
- Return the greatest possible product of n numbers from the array in JavaScript
- How to search an array for values present in another array and output the indexes of values found into a new array in MongoDB?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- Find and return array positions of multiple values JavaScript
- Specify return format in MongoDB to return the values as an array?
- Return an array populated with the place values of all the digits of a number in JavaScript
- Get greatest repetitive item in array JavaScript
- Comparing objects in JavaScript and return array of common keys having common values
- Return an array of all the indices of minimum elements in the array in JavaScript
- Greatest element in a Multi-Dimensional Array in JavaScript
- Greatest number in a dynamically typed array in JavaScript
- Sorting an array of binary values - JavaScript
- Make an array of another array's duplicate values in JavaScript
- Get only specific values in an array of objects in JavaScript?

Advertisements