0

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/

4
  • wouldn't it just be easier to create a single merged array, sort that, then pop off the final 5 elements? Commented Sep 3, 2014 at 17:03
  • @MarcB Well that's the idea, isn't it? Merge the arrays in descending order and stop when you hit 5 elements in the merged array? Commented Sep 3, 2014 at 17:06
  • yeah, but OP's doing a hell of a lot of extra work instead of a basic pop_5((array1 + array2 + array3).sort()) (pseudo code, of course) Commented Sep 3, 2014 at 17:07
  • hi thanks for answer why this function used inside the sort function function sortNumber(a, b) { return a - b; } Commented Sep 3, 2014 at 17:40

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Use var maxArray = flatArray.slice(0, 5); to extract top 5 elements.
@Plantface, why don't you stick with Jquery lib? I would have got some votes :-)
hi thanks for answer why this function used inside the sort function function sortNumber(a, b) { return a - b; }
@user2535959 If you leave that out, then it will use 'natural' sort order, meaning alphabetical. Numbers won't sort correctly in that case as the default sort function takes into account the length of the numbers.
@Plantface, I just edited the code to do the inverse sorting!
0

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.

Comments

0

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

2 Comments

note that the sort parameters ensure that the values are sorted in order (i.e. 2,3,7,8,12,12 etc. instead of .sort() which returns 12,12,12,2,3,7,8 because it sorts by the first digit).
You can't concatenate arrays with +. This is not PHP.
0

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);  
}

2 Comments

come now, that could have been a nice suggestion on the other answer :D
hi thanks for answer why this function used inside the sort function function sortInverse(a, b) { return a - b; }

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.