2

Assume we have the following multidimensional array:

const myArray = [[[1, 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]];

and I want to focus on element myArray[2][1], which is 7.

How can I find the single index of this element, despite the multidimensional array, in the fastest possible algorithm?

i.e. How can I find how many indivisible elements exist before this element?

So in the example above, the previous indivisible elements would be [0] = 1, [1] = 2, [2] = 3, [3] = 4, [4] = 5, [5] = 6 making the overall index of myArray[2][1], 6 .

Edit: Even though this example shows the largest dimension being 3-dimensions, is it possible to have an algorithm working for up to n-dimensions.

Ideally a function of the following form:

Array.prototype.getOverallIndex(i, j, k, ...)
{
...
return overallIndex;
}
13
  • Create a function that takes an array and indexes as arguments and returns the element at the specified indexes: function getElementAt(arr, i, j) { return arr[i][j]; } console.log(getElementAt(myArray, 2, 1)); // Output: 7 Commented Jan 22, 2023 at 12:28
  • 2
    How would you treat empty arrays? For example, this: [ [ [1, [ ] , [ [ [ [ ] ] ] ] , 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]] Commented Jan 22, 2023 at 12:28
  • please how do you get six as result? please add a detailed calculation for it. Commented Jan 22, 2023 at 12:33
  • If the output is 6 (the overall index of the input), then what is the input? [ 2, 1 ] (the index path) or 7 (the value)? This would make a difference for repeated values, e.g. for [ [ 1, 2 ], [ 3, 2 ] ], I’d expect the overall index of the path [ 1, 1 ] (representing array[1][1]) to be 3, but the overall index of the value 2 to be 1 (because it’s found at array[0][1]). Commented Jan 22, 2023 at 12:33
  • 1
    If the empty elements would be skipped, you might get away with using Array.prototype.flat(), but the trick would be in guessing the depth to pass to the .flat() method. EDIT It would also depend on your setup to adhere to these rules: i.imgur.com/ybBsdq1.png Commented Jan 22, 2023 at 12:46

2 Answers 2

3

Example:

const myArray = [[[1, 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]];
const flattened = myArray.flat(Infinity);
console.log(flattened.indexOf(7)) // 6

Then, if you want all the indexes of N, you could use:

const indexesOf = (arr, num) => arr.flat(Infinity).reduce((a, n, i) => (num === n && a.push(i), a), []);

// Use like:
const myArray = [[[1, 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]];
console.log(indexesOf(myArray, 7)) // [6, 7]

If in need to get the max depth of your array you could use:

const getDepth = (a) => Array.isArray(a) ? 1 + Math.max(0, ...a.map(getDepth)) : 0;

const myArray = [[[1, 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]];

// levels deep:
const depth = getDepth(myArray);
console.log(depth); // 3 

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

3 Comments

Ah, but what do we do if we were looking for the second 7? Or nth 7?
@FiddlingAway added another example that returns an Array of all the indexes where 7 is present in the flattened array. This way you can get the length of appearances and you can then query the nth.
@SUTerliakov you're absolutely right. Corrected as per docs examples.
1

You could count.

const
    getCount = v => Array.isArray(v)
        ? v.reduce((t, w) => t + getCount(w), 0)
        : 1,
    getCountOfItemsBefore = (array, target) => {
        let i = 0,
            count = 0;

        while (i < target[0]) count += getCount(array[i++]);

        if (target.length > 1 && Array.isArray(array[target[0]])) {
            count += getCountOfItemsBefore(array[target[0]], target.slice(1));
        }
        return count;
    },
    data = [[[1, 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]],
    target = [2, 1],
    result = getCountOfItemsBefore(data, target);

console.log(result);

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.