Return index of greatest value in an array
This is probably the best way, since it’s reliable and works on old browsers:
function indexOfMax(arr) {
if (arr.length === 0) {
return -1;
}
var max = arr[0];
var maxIndex = 0;
for (var i = 1; i < arr.length; i++) {
if (arr[i] > max) {
maxIndex = i;
max = arr[i];
}
}
return maxIndex;
}
There’s also this one-liner:let i = arr.indexOf(Math.max(...arr));
It performs twice as many comparisons as necessary and will throw a RangeError
on large arrays, though. I’d stick to the function. Javascript return all indexes of highest values in an array
Using Math.max
you can get the maximum element. Post that you map over the array and get the indices of this max element. The complexity of this approach is O(n);
const arr = [0,1,4,3,4];const max = Math.max(...arr);const res = [];arr.forEach((item, index) => item === max ? res.push(index): null);console.log(res);
How to find array index of largest value?
public int getIndexOfLargest( int[] array )
{
if ( array == null || array.length == 0 ) return -1; // null or empty
int largest = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[largest] ) largest = i;
}
return largest; // position of the first largest found
}
Finding Greatest value on array of indexes
You can do the following:
const findMax = (arr) => {
const max = Math.max.apply(Math, arr);
return arr.indexOf(max);
}
First you create a function that receives an array arr
then inside this function you find the array element with the highest value by using the JS built in Math.max method. If you return this value will show you the maximum value of the numbers in the array you've supplied.In order to return the index you can use the indexOf array method to find its index. You return this value and you have the index of the maximum number in an array.
Return index of highest value in an array
My solution is:
$maxs = array_keys($array, max($array))
Note:this way you can retrieve every key related to a given max value.
If you are interested only in one key among all simply use $maxs[0]
how can I get the index of the max value in an array?
If you do not want to use another class you can do the below.
You need to store both the max and the index of where max was so that you can also print the city
String[] city = getCities (); // assuming this code is done
int[] temp = getTemparatures ();
int max = Integer.MIN_VALUE;
int index = -1;
for(int i = 0; i < temp.length; i ++){
if(max < temp[i]){
max = temp[i];
index = i;
}
}
System.out.println ("The city with the highest average temperature is " + city[index] +
" with an average temperature of " + temp[index]);
How to find all positions of the maximum value in a list?
>>> m = max(a)
>>> [i for i, j in enumerate(a) if j == m]
[9, 12]
Related Topics
React Routing Works in Local MAChine But Not Heroku
Jquery: Change Event to Input File on Ie
What Is Event Pooling in React
Localstorage Object Is Undefined in Ie
Can Jquery Read/Write Cookies to a Browser
Quickest Way to Pass Data to a Popup Window I Created Using Window.Open()
Why Do I Need to Await an Async Function When It Is Not Supposedly Returning a Promise
Understanding JavaScript Promises; Stacks and Chaining
JavaScript Contenteditable - Set Cursor/Caret to Index
Should a Promise.Reject Message Be Wrapped in Error
Using Filesystem in Node.Js with Async/Await
Unexpected Token Illegal in Webkit
How to Stop Events Bubbling in Jquery
Why Is There a 'Null' Value in JavaScript
How to Read the Correct Time/Duration Values from Google Spreadsheet