Shruti
Shruti

Reputation: 1574

how to find largest elements from the sorted array?

I have three sorted array I need to find top five "5" element from these array .I am able to find first two element largest element .how I will find other ?

can you suggest how we can find other 3 element ?

here is my code

 var maxArray=[];
        var array1=[2,7,12,23,40,44,67,88,102]
        var array2=[3,12,14,17,23,40,41,67,108]
        var array3=[8,12,23,40,59,86,119,130]
        var firstMax=array1[array1.length-1];
        var secondMax=array2[array2.length-1];


        alert(array1[array1.length-1]);
        if(array1[array1.length-1]>array2[array2.length-1] && array1[array1.length-1]>array3[array3.length-1]){

            maxArray.push(array1[array1.length-1]) ;
            firstMax=array1[array1.length-1];
            if(array2[array2.length-1]>array3[array3.length-1]){
                secondMax=array2[array2.length-1];
            }else {
                secondMax=array3[array3.length-1];

            }
        }else if(array2[array2.length-1]>array1[array1.length-1]&& array2[array2.length-1]>array3[array3.length-1]){
            maxArray.push(array1[array2.length-1])
            firstMax=array2[array2.length-1];

            if(array1[array1.length-1]>array3[array3.length-1]){
                secondMax=array1[array1.length-1];
            }else {
                secondMax=array3[array3.length-1];

            }

        }else{
            maxArray.push(array3[array3.length-1])
            firstMax=array3[array3.length-1];

            if(array2[array2.length-1]>array1[array1.length-1]){
                secondMax=array2[array2.length-1];
            }else {
                secondMax=array1[array1.length-1];

            }

        }
        maxArray.push(secondMax)

        alert(maxArray)

fiddle http://jsfiddle.net/9vsjm8uh/

Upvotes: 0

Views: 109

Answers (4)

Benny Bottema
Benny Bottema

Reputation: 11493

jsFiddle (yep, even better without jQuery, thanks @Rajacsp)

var array1 = [2, 7, 12, 23, 40, 44, 67, 88, 102]
var array2 = [3, 12, 14, 17, 23, 40, 41, 67, 108]
var array3 = [8, 12, 23, 40, 59, 86, 119, 130]

var flatArray = array1.concat(array2).concat(array3);

flatArray.sort(function sortNumber(a, b) { return b - a; });
var maxArray = flatArray.slice(0, 5);

alert(maxArray); // 130,119,108,102,88

Upvotes: 2

Raja CSP
Raja CSP

Reputation: 172

Plain Javascript (no libraries added):

var array1=[2,7,12,23,40,44,67,88,102];
var array2=[3,12,14,17,23,40,41,67,108];
var array3=[8,12,23,40,59,86,119,130];


alert(getTopFive(array1, array2, array3));

function getTopFive(ar1, ar2, ar3){
    var finalArray = array1.concat(array2).concat(array3);
    finalArray.sort(function sortInverse(a,b) { return b - a; });
    return finalArray.slice(0, 5);  
}

Upvotes: 0

Michael Sacks
Michael Sacks

Reputation: 917

Combine all of the arrays, sort them, then get the last 5 values.

var total = array1.concat(array2, array3);
total = total.sort(function(a,b){return a-b});
//Now total[length-5] is the 5th largest value
//total[length-4] is the 4th largest and so on

Upvotes: 0

jj172
jj172

Reputation: 809

I would suggest the following idea:

Since you are looking for the top 5 values, they will, at worst case, all be in the same list. Thus, there are at most 5 * 3 = 15 values to check.

Then, you can take the 5 highest values from each list (which should be trivial if the list is already sorted), and then put them in another list. Now you have a list of the 15, and you want to find the top 5 values from this list. There are different ways to do this - you could sort the list, then take the top 5 values, or you can simply iterate through the list, finding the max each time.

Upvotes: 0

Related Questions