Reputation: 225
I have a array like
arr[1] = 234;
arr[2] = 345;
...
arr[40] = 126;
How can I get the index of the element with the highest value without reiterating the array?
Upvotes: 13
Views: 29268
Reputation: 6678
Is old question but here is an my simple emulation of PHP script max()
made in javascript:
function max(array){
if(Object.prototype.toString.call( array ) === '[object Array]'){
return array[(array.length-1)];
}
else return 0;
}
This return value of last key in array or 0
if nothing found.
Maby someone helps.
You can use it like:
var array = ['bananaman','spiderman','ironman','superman','batman','manman'];
var getLast = max(array);
if(getLast !== 0)
alert(getLast); // manman
Upvotes: 0
Reputation: 39
Get the array key with the highest value in javascript
var cars = ["Saab", "Volvo", "BMW"];
var max_car_result = cars[cars.length-1];
alert(max_car_result);
Upvotes: 3
Reputation: 3277
With jQuery, is as simple as:
// Get the max value from the array
maxValue = Math.max.apply(this, arr);
// Get the index of the max value, through the built in function inArray
$.inArray(maxValue,arr);
Upvotes: 10
Reputation: 104850
You can apply Math.max and pass the array as its arguments-
arr.indexOf(Math.max.apply(window,arr))
But now Math.max is doing the iterating, just as sort would do.
Somebody has to look at each item in an unsorted array...
Upvotes: 19
Reputation: 27470
Either you will have iteration somewhere (in your code or in JQuery.each()) or you can define something like this:
Array.prototype.mpush = function(v)
{
var maxv = this.maxValue || Number.MIN_VALUE;
if( v > maxv ) { this.maxValue = v; this.maxIndex = this.length; }
this.push(v);
}
and use that arr.mpush(v)
to populate your array. In this case the array will have maxIndex property.
Upvotes: 0
Reputation: 4085
You could use a function to set the variable. And keep track of the max in that function. Here's a quick example without type checking, testing, or support for removing a value.
Array.prototype.maxValue = null;
Array.prototype.setIndex = function(index, value){
this[index] = value;
if (value > this.maxValue || this.maxValue == null)
this.maxValue = value;
}
var arr = new Array();
arr.setIndex(0, 234);
arr.setIndex(1, 500);
arr.setIndex(2, -5);
var maxValue = arr.maxValue;
Obviously this is nicer if you're currently setting items like this:
var arr = new Array();
arr[0] = 1;
arr[1] = 500;
arr[2] = 2;
Rather than this:
var arr = { 1, 500, 2 };
The downside is its not natural and requires you to use function to get the correct results.
Upvotes: 2
Reputation: 62789
Keep the array sorted or use a heap.
Otherwise iterate. Even if you found some trick to do it it would still require iterating underneath so why not iterate?
If it seems like too much code, put it in a separate routine.
Upvotes: 1
Reputation: 723
Try this:
var max_index = -1;
var max_value = Number.MIN_VALUE;
for(var i = 0; i < arr.length; i++)
{
if(arr[i] > max_value)
{
max_value = arr[i];
max_index = i;
}
}
Upvotes: 2
Reputation: 2268
Two solution: to sort descending order and get the first element or:
function bigger(array) {
if (array.length < 1) {
return -1;
}
bigger = 0;
for(var i=1; i<array.length;i++ ) {
if(array[i] > array[bigger]) {
bigger = i;
}
}
return bigger;
}
you cold optimize using two variables, one for the position and other for the content.
Upvotes: 0
Reputation: 1039508
If the array is not ordered you cannot do this without iterating.
Upvotes: 4