0

There are 2 arrays data1 and data2. The desired result is an array containing all the elements in data1 which are larger than the largest element in data2 [+ the 3 before as defined in variable buffer]. In other words, the result should also include the previous 3 elements in data1 preceding the matched elements. Both arrays are already sorted in ascending order.

My code below does what is required, but is there a better way to do the same thing? Will regex be faster?

Data set

data1 = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
data2 = [102, 104, 106, 108]
buffer = 3

The desired result is [106, 107, 108, 109, 110]

Algorithm

largestData2 = _.max(data2)
data1LargerThanData2 = _.filter(data1, function(data1) {
    return data1 > largestData2
})
smallestData1LargerThanData2 = _.min(data1LargerThanData2)

sliceStart = data1.indexOf(smallestData1LargerThanData2) - buffer
result = data1.slice(sliceStart)
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • @KevinB I want all the elements in `data1` than are larger than any of the elements in `data2`, so I guess I will look for the largest element in `data2` to compare with all the elements from `data1` – Nyxynyx Jan 16 '14 at 16:23
  • @KevinB I'm also looking to include 3 other elements from `data1` than comes before `[106,107]`. Sorry I didnt explain it clearly, I have a variable `buffer = 3` which i used to find `sliceStart` – Nyxynyx Jan 16 '14 at 16:25
  • @KevinB Ah I guess you are right, its a wording problem. Let me edit the question – Nyxynyx Jan 16 '14 at 16:30
  • It currently outputs the correct result `[106, 107, 108, 109, 110]` – Nyxynyx Jan 16 '14 at 16:30

5 Answers5

1

JSFIDDLE DEMO

var data1 = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110],
data2 = [102, 104, 106, 108];
var maxData2 = Math.max.apply(Math, data2);

var newData = [];
data1.map(function(val) {
    if(val > maxData2) {
        newData.push(val);
    }
});
var index = data1.indexOf(newData[0]); //getting index of 1st element

var prev3Arr = [data1[index-3], data1[index-2], data1[index-1]]; //forming prev 3 elements

var finalResult = $.merge( prev3Arr, newData); // merging both arrays

console.log(finalResult);

Check your console for result: [106, 107, 108, 109, 110]

Venkata Krishna
  • 14,926
  • 5
  • 42
  • 56
1

Start by getting the largest value in data2:

var max = Math.max.apply(null, data2);

Then get all of the numbers from data1 that are greater than max:

var arr = data1.filter(function (el) {
  return el > max;
});

Use value of the first value in arr to find our starting point in data1 and take the appropriate chunk of elements from it.

var index = data1.indexOf(arr[0]) - 3;
var chunk = data1.slice(index, index + 3);

Merge arr into chunk.

chunk.push.apply(chunk, arr); // chunk = [106, 107, 108, 109, 110]

Demo.

Andy
  • 61,948
  • 13
  • 68
  • 95
1

Assuming the arrays are always in order:

var result;
var largestData2 = data2.slice( -1 );

_.each( data1, function( x, index ){
    if( x  > largestData2 ){
        result = data1.slice( index - buffer - 1 );
        return false;
    }
} );
Barney
  • 16,181
  • 5
  • 62
  • 76
1

If the two arrays are sorted ascending, you can omit max and just pick the last element

var largestData2 = data2[data2.length - 1];

To find the position in data1 being larger than largest_data2, you can use a binary search and then go back three elements

var smallestData1LargerThanData2Index = binarySearch(data1, largestData2);
var sliceStart = smallestData1LargerThanData2Index - buffer;
if (sliceStart < 0)
    sliceStart = 0;

var result = data1.slice(sliceStart);

JSFiddle

Update:

Updated JSFiddle to actually use underscore.js.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
-1

You want all the values from data1 that are larger than the largest value of data2? If so How might I find the largest number contained in a JavaScript array? this might help

Community
  • 1
  • 1
Beau
  • 50
  • 2