最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - finding the biggest difference between sorted array values - Stack Overflow

programmeradmin1浏览0评论

I have an array that may look like this...

var array = array(1,4,7,8,12,15);

The values in the array will always be integers, and always will go up, or could be the same as the previous, but at least won't bee less.

Now i would like to loop through the array and find out which "step" has the biggest difference between it, so in my example that would be array keys 4&5 since from 8-12 == 4... but it might also happen that this would be multiple steps with the same values, let's say my array would be this..

var array = array(1,5,7,8,12,15);

then it would be 0&1 and 4&5....

I'm looking for a function that can return me this... oh and it has to be PureJs...

I hope someone can help me.

I have an array that may look like this...

var array = array(1,4,7,8,12,15);

The values in the array will always be integers, and always will go up, or could be the same as the previous, but at least won't bee less.

Now i would like to loop through the array and find out which "step" has the biggest difference between it, so in my example that would be array keys 4&5 since from 8-12 == 4... but it might also happen that this would be multiple steps with the same values, let's say my array would be this..

var array = array(1,5,7,8,12,15);

then it would be 0&1 and 4&5....

I'm looking for a function that can return me this... oh and it has to be PureJs...

I hope someone can help me.

Share Improve this question edited Feb 17, 2017 at 13:43 Cerbrus 73k19 gold badges136 silver badges150 bronze badges asked Jun 18, 2013 at 14:50 Tobias HagenbeekTobias Hagenbeek 1,2133 gold badges16 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

I'm not sure exactly what you want. But if you want the greatest jump:

var max=0;
for (i=1; i<array.length; i++)
    max = Math.max(max,array[i]-array[i-1]);

If you want the first location at which this greatest jump occurs:

var max=0;
var firstLoc=0;
for (i=1; i<array.length; i++)
{
    curJump = array[i]-array[i-1];
    if (curJump > max) 
    {
        firstLoc = i;
        max = curJump;
    }
}

If you want the last location at which this jump occurs:

var max=0;
var lastLoc=0;
for (i=1; i<array.length; i++)
{
    curJump = array[i]-array[i-1];
    if (curJump >= max) 
    {
        lastLoc = i;
        max = curJump;
    }
}

If you want an array of all locations at which this max jump occurs:

var max=0;
var locs = [];
for (i=1; i<array.length; i++)
{
    curJump = array[i]-array[i-1];
    if (curJump == max)
    {
        locs.push(i);
    } else if (curJump > max)
    {
        locs = [i];
        max = curJump;
    }
}

a functional approach makes use of simple pure functions and built ins to do some of the heavy lifting:

var array = Array(1,5,7,8,12,15);

//get differences:
var diffs=array.map(function(a,b,c){
 return a - (c[b-1]||0);
},{});

//find max diff and collect elements:
var rez=diffs.map(function(a,b){return a==this && [b-1, b]; }, 
                   Math.max.apply(0, diffs)
            ).filter(Boolean);

//display the findings:
alert(rez.join("|")) //shows: 0,1|3,4

i'm always amazed when i get done and i don't see any local vars or conditionals being used...

Brute force solution is to go thru the array in sequence and see the difference from a[i] to [i+1]. if it is more than max, then set the max to this value.

A binary search might also be possible since the array is sorted.

发布评论

评论列表(0)

  1. 暂无评论