0

I have an array like

var arr = [ [1, 4, 5],
            [2, 6, 7],
            [3, 3, 9]]

I would like to get the row which has max value in column 2, so in this example row 2. How can I do this in javascript?

Edit: I can do it using a for loop which iterates over all rows and have a temp max variable to keep track of the max. But I was hoping for a more efficient way.

Thanks

5
  • 2
    Did you try something ? Like for example iterate over the array ? Commented Feb 25, 2014 at 12:21
  • Yes I can do it using a for loop which iterates over all rows and have a temp max variable to keep track of the max. But I was hoping for a more efficient way Commented Feb 25, 2014 at 12:22
  • 2
    That's the most efficient way. Other ones are just sweeter. Commented Feb 25, 2014 at 12:23
  • What would you like to have happen in the case of a tie? Commented Feb 25, 2014 at 12:31
  • @Will in case of a tie, the last found element will be used. Commented Feb 25, 2014 at 12:32

8 Answers 8

3

Well, iterating and keeping a temporary max variable is probably the most efficient way of doing it, but if you want a way that looks more pleasing, you can try something like:

var col2 = arr.map(function (elem) {
    return elem[1]; //to get all the column 2 values
});

var index = col2.indexOf(Math.max.apply(this, col2));

Edit: If you want to use the index of the last found element in case of a tie, use

col2.lastIndexOf(Math.max.apply(this, col2));

Sign up to request clarification or add additional context in comments.

Comments

1

The below code will give you the highest number:

var array = [[1, 4, 5],
            [2, 6, 7],
            [3, 3, 9]];
var bigNum = 0;
for(var i=0;i<array.length; i++){
    var largest = Math.max.apply(Math, array[i]);
    if(largest > bigNum) {
         bigNum = largest;   
    }
}
console.log(bigNum);

Comments

1

How about this

var arr = [[1, 4, 5], [2, 6, 7], [3, 3, 9]],
    t;

$.each(arr, function(k, v){
    t = !t ? v[1] : (v[1] > t ? v[1] : t);
});

console.log(t);

Comments

1

Nothing can out better plain loop version, but you can use Array.prototype.reduce like this

var arr = [ [1, 4, 5], [2, 6, 7], [3, 3, 9]], col = 1;

var best = arr.reduce(function(tillNow, current) {
    if (tillNow.best < current[col]) {
        tillNow.best = current[col];
        tillNow.row  = current;
    }
    return tillNow;
}, {best:0, row:[]});

console.log(best.row);    # [ 2, 6, 7 ]
console.log(best);        # { best: 6, row: [ 2, 6, 7 ] }

Reduce function accepts the till now value as the first parameter and the current element as the second parameter.

For the first element, parameters will be like this

tillNow : {best:0, row:[]} : current: [1, 4, 5]

We compare current's indented column with tillNow.best. If current's is bigger than tillNow, the best element and the current row will be set in tillNow and that will be returned which will be fed back into the reduce's next iteration as tillNow. So, in our case, on the second iteration, values change like this

tillNow : {best:4, row: [1, 4, 5]} : current: [2, 6, 7]

And on third iteration,

tillNow : {best:6, row: [2, 6, 7]} : current: [3, 3, 9]

And finally the result will be

{ best: 6, row: [ 2, 6, 7 ] }

Comments

1

Please try this.

var col2 = [];
var max = 0;

$.each(arr, function(i, val){
    col2.push(val[1]);
})

max = Math.max.apply( Math, col2 );

Comments

1

This example returns runs map over over each row and returns an array with the each row's number from the nominated column. It then returns the index of the highest number in that array.

function findRowForMaxInCol(arr, col) {
  var column = arr.map(function(el) { return el[col]; });
  var highest = Math.max.apply(null, column);
  return column.indexOf(highest);
}

console.log(findRowForMaxInCol(arr, 1)); // 1

Fiddle.

1 Comment

You get the maximum value, but how to get the row in which it sits?
0

Just iterating through the array and checking if the second element is largest. The variable large will contain the row i and the largest value e.

var arr = [ [1, 4, 5],
        [2, 6, 7],
        [3, 3, 9]],
    large = {i:0, e: 0};  // i: row, e: largest elem
arr.map(function (a, idx) {
    if (a[1] > large.e) { 
        large.e = a[1];
        large.i = idx;
    }
});
console.log(large);  // {i: 1, e: 6}. row: 1 and element 6.

1 Comment

returns (0, 0) if array is []
0

Generalizing the problem: find the element in an array which has the maximum 'f(x)':

var findMax = function(array, f){
    var element = null;
    var max = -Infinity;
    for (var i=0; i!=array.length; ++i){
        var value = f(array[i]);
        if (value > max){
            element=array[i];
            max=value;
        }
    };
    return element;
};

And apply the general algorithm to rows with a function returning the second element of the row:

var secondElement= function(row){ return row[1]; };

var max = findMax(rows, secondElement);

(JSFiddle)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.