Returning array of largest numbers

13aal

I have this little script that will return the largest number of an array, it works, except it doesn't work for multidimensional arrays.

How can I tweak this to return an array of the largest numbers within a multidimensional array?

Source:

function largestOfFour(arr) {
    for(var x=0;x < arr.length; x++){
        var largest = Math.max.apply(Math, arr[x]);
        return largest;
    }
}

Example:

> function largestOfFour(arr) {
...     for(var x=0;x < arr.length; x++){
.....         var largest = Math.max.apply(Math, arr[x]);
.....         return largest;
.....     }
... }
undefined
> largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
5
>

Expected output:

[5, 27, 39, 1001]
Nina Scholz

Just map the result of the single results.

function largestOfFour(array) {
    return array.map(function (a) {
        return Math.max.apply(null, a);
    });
}

var largest = largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
document.write('<pre>' + JSON.stringify(largest, 0, 4) + '</pre>');

The above mentioned (@Tushar) short version

function largestOfFour(array) {
    return array.map(Math.max.apply.bind(Math.max, null));
}

var largest = largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
document.write('<pre>' + JSON.stringify(largest, 0, 4) + '</pre>');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extract largest numbers from an array

From Dev

Find largest and smallest numbers in an array

From Dev

Scheme: Returning two largest numbers from a set of three numbers

From Dev

Finding the Largest increase in an array filled with random numbers

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Largest 5 in array of 10 numbers without sorting

From Dev

Find the largest sequence in a given array of numbers

From Dev

find the largest three Numbers on the array of int swift

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Finding the 3 largest numbers in an array/list of n numbers without the sorting

From Dev

Find largest consecutive numbers in array and output numbers and how many there is

From Dev

C find the 2 largest numbers and 2 smallest numbers in float array

From Dev

How to find 3 largest even numbers in an array with C?

From Dev

Find Two largest numbers in a list without using Array

From Dev

Find the order of numbers which will give the largest number in an array

From Dev

Print the Sum of the largest of two numbers with the collections of array method?

From Dev

C largest sum of 3 consecutive numbers from array

From Dev

Javascript how to find largest numbers in array, and record positions

From Dev

Efficient way of finding the 5 largest numbers in an array at any given interval

From Dev

Using sort to return an array of largest numbers from subarrays

From Java

Write a program to find 100 largest numbers out of an array of 1 billion numbers

From Dev

Returning largest value in javascript object

From Dev

Go output with ioutil: returning array of (ASCII?) numbers from http call

From Dev

MongoDb returning only specific fields (phone numbers) in the form of an array

From Dev

Returning the highest value of an array after inputting 5 numbers

From Dev

MS EXCEL: Returning quartile bounds and values from an array of numbers

From Dev

MongoDb returning only specific fields (phone numbers) in the form of an array

From Dev

Fastest way to find the third largest of five given numbers without using array or loops?

From Dev

Is it possible to find the largest drop between two numbers in an array in less than O(n²) complexity?

Related Related

  1. 1

    Extract largest numbers from an array

  2. 2

    Find largest and smallest numbers in an array

  3. 3

    Scheme: Returning two largest numbers from a set of three numbers

  4. 4

    Finding the Largest increase in an array filled with random numbers

  5. 5

    Java - Finding Largest and Smallest Numbers using an Array

  6. 6

    Largest 5 in array of 10 numbers without sorting

  7. 7

    Find the largest sequence in a given array of numbers

  8. 8

    find the largest three Numbers on the array of int swift

  9. 9

    Java - Finding Largest and Smallest Numbers using an Array

  10. 10

    Finding the 3 largest numbers in an array/list of n numbers without the sorting

  11. 11

    Find largest consecutive numbers in array and output numbers and how many there is

  12. 12

    C find the 2 largest numbers and 2 smallest numbers in float array

  13. 13

    How to find 3 largest even numbers in an array with C?

  14. 14

    Find Two largest numbers in a list without using Array

  15. 15

    Find the order of numbers which will give the largest number in an array

  16. 16

    Print the Sum of the largest of two numbers with the collections of array method?

  17. 17

    C largest sum of 3 consecutive numbers from array

  18. 18

    Javascript how to find largest numbers in array, and record positions

  19. 19

    Efficient way of finding the 5 largest numbers in an array at any given interval

  20. 20

    Using sort to return an array of largest numbers from subarrays

  21. 21

    Write a program to find 100 largest numbers out of an array of 1 billion numbers

  22. 22

    Returning largest value in javascript object

  23. 23

    Go output with ioutil: returning array of (ASCII?) numbers from http call

  24. 24

    MongoDb returning only specific fields (phone numbers) in the form of an array

  25. 25

    Returning the highest value of an array after inputting 5 numbers

  26. 26

    MS EXCEL: Returning quartile bounds and values from an array of numbers

  27. 27

    MongoDb returning only specific fields (phone numbers) in the form of an array

  28. 28

    Fastest way to find the third largest of five given numbers without using array or loops?

  29. 29

    Is it possible to find the largest drop between two numbers in an array in less than O(n²) complexity?

HotTag

Archive