Question

var set = new Array(1, 2, 1, 2, 3, 9, 12, 15);

I need to extract largest numbers from this kind of set.
How many numbers are extracted doesn't matter
but they need to have significant difference with the unextracted ones.

So in this set I should get [9, 12, 15]

I don't even know how to start.

Était-ce utile?

La solution 3

Aternative way of choosing largest numbers:

var set = new Array(1, 2, 1, 2, 3, 9, 12, 15);

function getHighNums(arr,percentage,fillpercentage){
    var perc=0;
    var sorted=arr.sort(function(a,b){ return a-b});
    var total=0;
    for(var i=0;i<arr.length;i++) total+=arr[i];
    for(var j=sorted.length-1;j>=0;j--){
        perc+=sorted[j]/total*100;
        if(fillpercentage){
            if(perc > percentage) return sorted.slice(j,sorted.length);
        }else{ 
            if(sorted[j]/total*100 < percentage) return sorted.slice(j+1,sorted.length);
        }
    }
    return sorted;

}

console.log(getHighNums(set, 10, false))//9,12,15
console.log(getHighNums(set, 50, true))//12,15

The first line gets all numbers that are at least 10% of sum of array value. Sum of array= 1+2+1+2+3+9+12+15=45 so it picks numbers > 4.5

The second line gets numbers until their sum is at least 50% of the total sum of the array. So as sum of array =45, it will pick highest numbers until their sum is > 22.5

Autres conseils

If, as you seem to indicate in a comment to your question, you just want those greater than the third quartile, it's easy.

Simply create a sorted list of the N numbers and then get those above the 3N/4 index position.

You can use Array.sort() to sort the array, Array.length to get the length and Array.slice() to extract a slice of the array.

For example, the following code:

var set = new Array(1, 2, 1, 2, 3, 9, 12, 15);
document.write(set);

set.sort(function(a, b){return a-b});
document.write('<br>');
document.write(set);

var len = set.length;
document.write('<br>');
document.write(len);

var topQ = set.slice (3*len/4);
document.write('<br>');
document.write(topQ);

outputs the unsorted and sorted list, the length, and the top 25%:

1,2,1,2,3,9,12,15
1,1,2,2,3,9,12,15
8
12,15

Here is your solution to get max 3 number of max value

 var set = new Array(1, 2, 1, 2, 3, 9, 12, 15);


function getmax(index)
{ return set.sort(function(a,b){return b-a;})[index];

}

var topmax = new Array(
getmax(2),getmax(1),getmax(0)
)


console.log(topmax);

JSBIN Example

EDITED

var set = new Array(1, 2, 1, 2, 3, 9, 12, 15);

 set.sort(function(a,b){return b-a;});



document.write(set[2],set[1],set[0]);

You can try this. (look for value that is less than CV - CV / I; Where CV - current value; I - some coefficient)

Javascript

var data = [1, 2, 1, 2, 3, 9, 12, 15],
    index = 3,
    i;

function getMaximums(data, index){
    data = data.sort(function(a, b){return a < b});

    for(i = 0; i < data.length; i++){
        if(!data[i + 1] || (data[i] - data[i] / index) > data[i + 1]) return data.splice(0, i + 1);
    }
}

console.log(getMaximums(data, index));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top