0

    function theHighest(data) {
        let twoLargest = data.map((x) => {
            return x.reduce((prev, curr) => {
                return curr
            })
        })
        return twoLargest //returns [3,5,8]
    
    }
    console.log(theHighest([[1, 2, 3], [3, 4, 5], [6, 7, 8]]))

The above function can return the largest numbers in each array and if it could return prev along with curr in the same array the job would be done and the desired result would be achieved which is [2,3,4,5,7,8] How can I return this without using for loops at all?

If I use for loops here is how I do it:

    function theHighest(data) {
        let highestValues = []
    
        for (let i = 0; i < data.length; i++) {
            let first = 0
            let second = 0
             
            for (let j = 0; j < data[i].length; j++) {
                    if (first < data[i][j]) {                  
                        second = first;
                        first = data[i][j];
                    }
                    else if (second < data[i][j]) {                   
                        second = data[i][j];
                    }
    
            }
            highestValues.push(first, second)
        }    
    return highestValues
        }
    console.log(theHighest([[1, 2, 3], [3, 4, 5], [6, 7, 8]]))

Thank you!

3
  • 1
    The first function does not return the largest number of each array, it returns the last number (which is different in general unless the arrays are already sorted). Commented Feb 12, 2018 at 10:13
  • Either way, that prev in your reduce function is the output of the previous iteration. You can set it to an array of the largest two numbers so far. Commented Feb 12, 2018 at 10:14
  • If the arrays are indeed already sorted, you can map a slice that takes the last two entries. Commented Feb 12, 2018 at 10:18

3 Answers 3

2

You could take a copy, sort the array and return the two max values.

function theHighest(data) {
    return [].concat(...data.map(a => a.slice().sort((a, b) => a - b).slice(-2)));
}

console.log(theHighest([[1, 2, 3], [3, 4, 5], [6, 7, 8]]));

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

1 Comment

or just [...a].sort() ;)
2

You need to sort the array as well if it not sorted

function theHighest(data) {
        let twoLargest = data.map((x) => {
            // Get two largest integers
            return x.sort().slice(-2);
        })
        // Flatten the array
        return Array.prototype.concat(...twoLargest);
    
    }
   
    console.log(theHighest([[1, 2, 3], [3, 4, 5], [6, 7, 8]]))

1 Comment

@Thilo ah ignore that.
1

You can also use reduce and sort

var output = arr.reduce( (a, c) => a.concat(c.sort().slice(-2)), [] );

outputs [2,3,4,5,7,8]

Demo

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

var output = arr.reduce( (a, c) => a.concat(c.sort().slice(-2)), [] );

console.log( output );

4 Comments

I'm sure I have seen this approach before. Sure you don't want to look around and mark it as dupe if this is the ideal solution?
@Thilo That is exactly what my answer does.. It returns [3.5.8] which is the largest of each array
@Rajesh Sure looking for one... If I find exact duplicate I will delete this one
@Thilo Thanks for pointing out, didn't spotted it first time... it was there in the title itself.

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.