Alexa
Alexa

Reputation: 225

Get the array key with the highest value in javascript

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

Answers (10)

Ivijan Stefan Stipić
Ivijan Stefan Stipić

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

Kaushikkumar Kothiya
Kaushikkumar Kothiya

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

Dario Barrionuevo
Dario Barrionuevo

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

kennebec
kennebec

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

c-smile
c-smile

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

John Hoven
John Hoven

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

Bill K
Bill K

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

thomasa88
thomasa88

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

Pih
Pih

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

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

If the array is not ordered you cannot do this without iterating.

Upvotes: 4

Related Questions