0

I have an array of this:

[34, 12, 56]
[100,125,19]
[30,50,69]

125 has been the highest value, it will return the index [1,1] format. Meaning 125 which is the highest value will return row 1 column 1

I was able to get the index in an array using this code

var a = [0, 21, 22, 7, 12];
var indexOfMaxValue = a.reduce((iMax, x, i, arr) => x > arr[iMax] ? i : 
iMax, 0);
document.write("indexOfMaxValue = " + indexOfMaxValue); // prints 
"indexOfMaxValue = 2"
1
  • 1
    That's not 3 dimensional - only 2d. Commented Sep 12, 2019 at 8:57

3 Answers 3

2

Here's my approach. It flattens out all the arrays into more managable one, finds the max number and its index, and then calculates it's position using some math. Using a single array makes this calculation much easier.

const arr = [[34, 12, 56], [100,125,19], [30,50,69]];
const arr2 = [0, 21, 22, 7, 12];

function findHighest(arr) {

  // Get the number of columns
  const cols = arr.length;

  // Flatten out the arrays
  const tempArr = arr.flatMap(el => el);

  // Get the max number from the array
  const max = Math.max.apply(null, tempArr);

  // Find its index
  const indexMax = tempArr.findIndex(el => el === max);

  // Find the remainder (modulo) when you divide the index
  // by the number of columns
  const mod = indexMax % cols;

  // Return the final array output
  return [Math.floor(indexMax / cols), mod];
}

console.log(findHighest(arr))
console.log(findHighest(arr2))

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

2 Comments

Pretty sweet solution, but have in mind if the array's arent the same length this might break
That's a fair point @SimonDragsbæk, but sometimes you just have to go with the limited information in the question.
0

This will give the expected output but not sure is it good way to solve this:

var arr = [
    [34, 12, 56],
    [100, 125, 19],
    [30, 50, 69]
];
var maxValue, maxIndex;
arr.forEach((arr1, i) => {
    arr1.forEach((value, j) => {
        if (i == 0 && j == 0) {
            maxValue = value;
            maxIndex = [i, j]
        } else {
            if (maxValue < value) {
                maxValue = value;
                maxIndex = [i, j];
            }

        }

    });
});

console.log("Max Number Index", maxIndex);

2 Comments

This doesn't care about the array lengths it just finds the highest value
You can't just edit the question to fit your answer. That's not how this works.
0

If you mean 2d solution, try this. Should work for dynamic length arrays This should be extendable with a new forEach for a new dimension

[100,125,19],
[30,50,69]];

maxIndex = [-1, -1];
maxElem = 0;
input.forEach(function(arr, row) {
    console.error(row);
    arr.forEach(function(e, col) {
    if( maxElem <= e ) {
        maxElem = e;
        maxIndex = [row, col];
    }
  })
})

console.log(maxIndex)

1 Comment

Array.map will iterate the array but I guess for/forEach sustainable for iteration not Array.map

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.