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
}
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]);
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 to find the index of the max value in a list for Python?
- You are trying to find the index of
i
. It should belist_c[i]
.
Easy way/Better way is:
idx_max = i
. (Based on @Matthias comment above.)
- Use
print
to print the results and notreturn
. You usereturn
inside functions. - Also your code doesn't work if
list_c
has all negative values because you are settingmax_val
to0
. You get a 0 always.
list_c = [-14, 7, -9, 2]
max_val = 0
idx_max = 0
for i in range(len(list_c)):
if list_c[i] > max_val:
max_val = list_c[i]
idx_max = i
print(list_c, max_val, idx_max)
[-14, 7, -9, 2] 7 1
Getting the index of the returned max or min item using max()/min() on a list
if is_min_level:
return values.index(min(values))
else:
return values.index(max(values))
Related Topics
How to Get JavaScript Function Data into a PHP Variable
Why Is Pdo Better For Escaping MySQL Queries/Querystrings Than MySQL_Real_Escape_String
How to Prevent the "Confirm Form Resubmission" Dialog
Why Is This PHP Call to Json_Encode Silently Failing - Inability to Handle Single Quotes
Get Absolute Path of Initially Run Script
Safely Catch a 'Allowed Memory Size Exhausted' Error in PHP
Convert Var_Dump of Array Back to Array Variable
PHP Pdo Prepared Statement - MySQL Like Query
Login Without Https, How to Secure
What's the Difference Between ++$I and $I++ in PHP
How to Run a PHP Script Inside a HTML File
PHP Convert Date Format Dd/Mm/Yyyy =≫ Yyyy-Mm-Dd